toBinaryString() method

Discussion in 'Java' started by PaowZ, Dec 16, 2006.

  1. PaowZ

    PaowZ Guest

    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.
     
    PaowZ, Dec 16, 2006
    #1
    1. Advertising

  2. PaowZ wrote:
    >
    > 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
     
    Thomas Hawtin, Dec 16, 2006
    #2
    1. Advertising

  3. PaowZ

    PaowZ Guest

    Thomas Hawtin a écrit :

    > PaowZ wrote:
    > >
    > > 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. :)
     
    PaowZ, Dec 17, 2006
    #3
  4. PaowZ wrote:
    > Thomas Hawtin a écrit :
    >
    >> PaowZ wrote:
    >>> 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
     
    Thomas Hawtin, Dec 17, 2006
    #4
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. jerry051
    Replies:
    2
    Views:
    756
    Oliver Sturm
    Aug 2, 2005
  2. Mike Wilmot
    Replies:
    0
    Views:
    3,292
    Mike Wilmot
    Dec 15, 2003
  3. CW
    Replies:
    0
    Views:
    566
  4. Carlos Oliveira
    Replies:
    0
    Views:
    18,969
    Carlos Oliveira
    Aug 19, 2004
  5. Kyung won Cheon
    Replies:
    0
    Views:
    208
    Kyung won Cheon
    Nov 21, 2008
Loading...

Share This Page