# Thread: Convert 4275878552 to binary of length 4

1. Member
Join Date
Mar 2010
Posts
11
Rep Power
0

## Convert 4275878552 to binary of length 4

Hi,

I want to convert 4275878552 to a byte array of length 4. Please help.

I have used below code but does not work:

buffer[0] = (byte) ((nMarkerValue >> 24) & 0xff);
buffer[1] = (byte) ((nMarkerValue >> 16) & 0xff);
buffer[2] = (byte) ((nMarkerValue >> 8) & 0xff);
buffer[3] = (byte) ((nMarkerValue) & 0xff);

Thanks,

2. Originally Posted by sm123
Hi,

I want to convert 4275878552 to a byte array of length 4. Please help.

I have used below code but does not work:

buffer[0] = (byte) ((nMarkerValue >> 24) & 0xff);
buffer[1] = (byte) ((nMarkerValue >> 16) & 0xff);
buffer[2] = (byte) ((nMarkerValue >> 8) & 0xff);
buffer[3] = (byte) ((nMarkerValue) & 0xff);

Thanks,
Define "does not work".

kind regards,

Jos

3. Member
Join Date
Mar 2010
Posts
11
Rep Power
0
Gives me wrong value.

4. Originally Posted by sm123
Gives me wrong value.
Well, that number in hexadecimal gives 0xfedcba98 so the four bytes should be 0xfe, 0xdc, 0xba and 0x98. What did you get?

kind regards,

Jos

5. Member
Join Date
Mar 2010
Posts
11
Rep Power
0
I am getting values :

-2
-36
-70
-104

6. Originally Posted by sm123
I am getting values :

-2
-36
-70
-104
That is correct; bytes are also signed and are numbers in the range [-128, 127]. Add 256 to each of your numbers and see for yourself. (or make your buffer and int[]).

kind regards,

Jos

7. Member
Join Date
Mar 2010
Posts
11
Rep Power
0
Thanks. It did work. I misinterpreted the bytes.

8. Originally Posted by sm123
Thanks. It did work. I misinterpreted the bytes.
Good; also have a look at the ByteBuffer class: it can do all that for you. If you have a byte[] already you can wrap a ByteBuffer around it or you can make it allocate it's own byte[]. You can stick ints, longs, doubles etc. in the buffer or you can read these types from it. You can also get the 'backing' byte[] out of it again. Much easier than doing it all yourself and less error prone.

kind regards,

Jos

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•