I'm reading a binary file like this:
InputStream in = new FileInputStream( file );
byte[] buffer = new byte[1024];
while( ( in.read(buffer ) > -1 ) {
int a = // ???
}
What I want to do it to read up to 4 bytes and create a int value from those but, I don't know how to do it.
I kind of feel like I have to grab 4 bytes at a time, and perform one "byte" operation ( like >> << >> & FF and stuff like that ) to create the new int
What's the idiom for this?
EDIT
Ooops this turn out to be a bit more complex ( to explain )
What I'm trying to do is, read a file ( may be ascii, binary, it doesn't matter ) and extract the integers it may have.
For instance suppose the binary content ( in base 2 ) :
00000000 00000000 00000000 00000001
00000000 00000000 00000000 00000010
The integer representation should be 1
, 2
right? :- / 1 for the first 32 bits, and 2 for the remaining 32 bits.
11111111 11111111 11111111 11111111
Would be -1
and
01111111 11111111 11111111 11111111
Would be Integer.MAX_VALUE ( 2147483647 )
try something like this:
this is assuming that the lowest byte comes first. if the highest byte comes first you might have to swap the indices (go from 0 to 3).
basically for each byte you want to add, you first multiply a by 256 (which equals a shift to the left by 8 bits) and then add the new byte.
For reading unsigned 4 bytes as integer we should use a long variable, because the sign bit is considered as part of the unsigned number.
This is tested well worked function
If you have them already in a byte[] array, you can use:
source: here
You should put it into a function like this:
Example:
Output:
This takes care of running out of bytes and correctly handling negative byte values.
I'm unaware of a standard function for doing this.
Issues to consider:
Endianness: different CPU architectures put the bytes that make up an int in different orders. Depending on how you come up with the byte array to begin with you may have to worry about this; and
Buffering: if you grab 1024 bytes at a time and start a sequence at element 1022 you will hit the end of the buffer before you get 4 bytes. It's probably better to use some form of buffered input stream that does the buffered automatically so you can just use
readByte()
repeatedly and not worry about it otherwise;Trailing Buffer: the end of the input may be an uneven number of bytes (not a multiple of 4 specifically) depending on the source. But if you create the input to begin with and being a multiple of 4 is "guaranteed" (or at least a precondition) you may not need to concern yourself with it.
to further elaborate on the point of buffering, consider the
BufferedInputStream
:Now you have an
InputStream
that automatically buffers 1024 bytes at a time, which is a lot less awkward to deal with. This way you can happily read 4 bytes at a time and not worry about too much I/O.Secondly you can also use
DataInputStream
:or even:
and not worry about constructing
int
s at all.You can also use BigInteger for variable length bytes. You can convert it to Long, Integer or Short, whichever suits your needs.
or to denote polarity: