Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / December 2006

Tip: Looking for answers? Try searching our database.

toBinaryString() method

Thread view: 
PaowZ - 16 Dec 2006 22:10 GMT
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.
Thomas Hawtin - 16 Dec 2006 22:38 GMT
>    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
PaowZ - 17 Dec 2006 11:07 GMT
Thomas Hawtin a ?crit :

> >    1.
> >       byte buffer[] = new byte[3]; //my 3-bytes-long buffer
[quoted text clipped - 4 lines]
> 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
[quoted text clipped - 8 lines]
>   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. :)
Thomas Hawtin - 17 Dec 2006 14:43 GMT
> Thomas Hawtin a écrit :
>
>>>       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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.