简体   繁体   中英

Java - Casting Int to Byte - odd results

I'm performing bitwise operations on blocks 4-bytes large. I've cast the 4-byte blocks into 4-byte integers to do this, using ByteBuffer.

Once they are done, I need to cast them back in to bytes. Using ByteBuffer, I'm performing this. Here's some sample code, with data, to highlight my confusion:

int a = 1610612739; //simulate casting the 4-byte  block 60 00 00 03
ByteBuffer b = ByteBuffer.allocate(4);
b.putInt(a);
byte[] b1 = (b.array());

Now, after this code I expect b1 to hold the following:

b1[0]: 60
b1[1]: 00
b1[2]: 00
b1[3]: 03

But that's not what's happening. Instead, I get

b2[0]: 96
b2[1]: 0
b2[2]: 0
b2[3]: 3

My sample integer, in binary, should be::

0110 0000 0000 0000 0000 0000 0000 0011 where the far left bit is the sign.

and 96 00 00 03 in decimal is 2,516,582,403 which is outside Java's 32-bit int range.

Does anyone know what's going on?

The problem is that, you think it is supposed to show in Hexadecimal but it shows in decimals . That's it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM