toBinaryString() method

P

PaowZ

Hi there!

I'm trying using this method but I'm experimenting some issues. I
explain:


1.
byte buffer[] = new byte[3]; //my 3-bytes-long buffer
2.
res = in.read(buffer); // I read 3 bytes from file to
put in the array
3.
String bloc1 = Integer.toBinaryString(buffer[0]); // I want
the first byte to be represented in string format
4.
String bloc2 = Integer.toBinaryString(buffer[1]); // the
second byte
5.
String bloc3 = Integer.toBinaryString(buffer[2]); // the
third byte



This is what I get, when it works:

1.
01010010 | 01100001 | 01110010



Here, no problem.. I get 3 bytes, I mean 24 bits in string format.

After some iterations (always the same) I get weird thing, such as:

1.
00000000 | 11111111111111111111111111001111 |
11111111111111111111111110010000


As you can see, two string formatted bytes are not bytes. Indeed,
they're 32 bits long.


Do you have any idea of this trouble using toBinaryString ?

Thanks a lot :)

V.
 
T

Thomas Hawtin

PaowZ said:
1.
byte buffer[] = new byte[3]; //my 3-bytes-long buffer
2.
res = in.read(buffer); // I read 3 bytes from file to
put in the array

That may not read 3 bytes. Either check res and use the read(byte[]
buff, int off, int len) form or wrap in DataInputStream and use readFully.
3.
String bloc1 = Integer.toBinaryString(buffer[0]); // I want
After some iterations (always the same) I get weird thing, such as:

1.
00000000 | 11111111111111111111111111001111 |
11111111111111111111111110010000

That will be negative numbers. byte are signed. When you cover -1 (0xff)
from a byte to an int you preserve the sign to get -1 (0xffffffff). To
get the low order octet use b & 0xff.

Tom Hawtin
 
P

PaowZ

Thomas Hawtin a écrit :
PaowZ said:
1.
byte buffer[] = new byte[3]; //my 3-bytes-long buffer
2.
res = in.read(buffer); // I read 3 bytes from file to
put in the array

That may not read 3 bytes. Either check res and use the read(byte[]
buff, int off, int len) form or wrap in DataInputStream and use readFully.

I check for several iterations, including when I get negative byte..For
each loop, I really fetch 3 bytes. Not more, not less :)

3.
String bloc1 = Integer.toBinaryString(buffer[0]); // I want
After some iterations (always the same) I get weird thing, such as:

1.
00000000 | 11111111111111111111111111001111 |
11111111111111111111111110010000

That will be negative numbers. byte are signed. When you cover -1 (0xff)
from a byte to an int you preserve the sign to get -1 (0xffffffff). To
get the low order octet use b & 0xff.

Yes, I believe problems come from negative numbers..
What do you mean by 'low order octet use b & 0xff' ??
I must use a 'and operation' between the byte I read and 0xff ??

thanks a lot. :)
 
T

Thomas Hawtin

PaowZ said:
Thomas Hawtin a écrit :
PaowZ said:
res = in.read(buffer); // I read 3 bytes from file to
put in the array
That may not read 3 bytes. Either check res and use the read(byte[]
buff, int off, int len) form or wrap in DataInputStream and use readFully.

I check for several iterations, including when I get negative byte..For
each loop, I really fetch 3 bytes. Not more, not less :)

With something like:

int bytesRead = 0;
do {
int res = in.read(buffer, bytesRead, buffer.length);
if (res == -1) {
throw new EOFException();
}
bytesRead += res;
} while (bytesRead < buffer.length);

? I think DataInputStream.readFully is easier.
Yes, I believe problems come from negative numbers..
What do you mean by 'low order octet use b & 0xff' ??
I must use a 'and operation' between the byte I read and 0xff ??

If you only want the least significant eight bits (octet) set you need
to clear the other 24 bits of the int. The obvious way to do this is
with a bitwise and operation. Another way is to shift the bits 24 to the
left and then back down (without sign extension): (b << 24) >>> 24.

Tom Hawtin
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top