Java - converting byte[] to int not giving result -


i have hexbinary of 4 bytes follows:

ffffffc4 

it should return big following function gives -60:

public static int bytearraytoint(byte[] b)      {         return   b[3] & 0xff |                 (b[2] & 0xff) << 8 |                 (b[1] & 0xff) << 16 |                 (b[0] & 0xff) << 24;     } 

why doesn't work? doing wrong?

the primitive type int 32-bits long , significative bit sign. value ffffffc4 has msb set 1, represents negative number.

you can "something big" using long instead of int:

public static long bytearraytoint(byte[] b)  {     return  (((long) b[3]) & 0xff) |             (((long) b[2]) & 0xff) << 8 |             (((long) b[1]) & 0xff) << 16 |             (((long) b[0]) & 0xff) << 24; } 

Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -