Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Convert byte array to an integer/long/double

Status
Not open for further replies.

TheObserver

Programmer
Mar 26, 2002
91
0
0
US
I have a byte array I'm reading in from a file. I need to be able to take n bytes from that array and convert them into an integer/long/double, such that the resultant value is equal to the four bytes.

For example, if I have these values in my byte array:
10100111
00001010
11100110
10001010

I would like to read them like this:
10100111000010101110011010001010

And produce the value:
2802509450

Thanks for your time.
 
When you say byte array you mean

Code:
byte[] data;
?

Any code snippet would be nice.

Cheers,
Dian
 
Here's what I've got...

[tt]
File file = new File(filein);
InputStream is = new FileInputStream(file);


// Get the size of the file
long length = file.length();


// Check to ensure that file is not larger than MAX_VALUE.
if (length > MAX_VALUE)
{
// File is too large
System.out.println("The File is bigger than expected.\n");
}


// Create the byte array to hold the data
byte[] bytes = new byte[(int)length];

[/tt]
 
Nice.

And what is supposed to be inside the file?

Cheers,
Dian
 
Thanks for your help...I figured it out. I'm taking the byte, casting it to an int, and multiplying by 256 to shift the value over then adding it to a local scope variable.

Thanks for your time.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top