# Convert 4275878552 to binary of length 4

• 06-30-2010, 11:20 AM
sm123
Convert 4275878552 to binary of length 4
Hi,

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,
• 06-30-2010, 11:29 AM
JosAH
Quote:

Originally Posted by sm123
Hi,

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
• 06-30-2010, 11:31 AM
sm123
Gives me wrong value.
• 06-30-2010, 11:35 AM
JosAH
Quote:

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
• 06-30-2010, 11:42 AM
sm123
I am getting values :

-2
-36
-70
-104
• 06-30-2010, 11:52 AM
JosAH
Quote:

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
• 06-30-2010, 11:56 AM
sm123
Thanks. It did work. I misinterpreted the bytes.
• 06-30-2010, 12:08 PM
JosAH
Quote:

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