-
Im trying to convert an integer of size 4 to byte array but its
giving me strange value.byte[] bytes1 = my_int_to_bb_le(196,4); public static byte[] my_int_to_bb_le(int myInteger,int length) { return ByteBuffer.allocate(length).order(ByteOrder.LITTLE_ENDIAN).putInt(myInteger).array(); } -
output :
-60 0 0 0 -
required output as : 196 0 0 0
Tried the alternate this code but not working as its giving the same
outputpublic static byte[] convertIntByteArray(int intval,int len) { byte[] intarray = new byte[len]; for(int i=0;i<len;i++) { if(i==0) { intarray[i]=(byte)intval; } else intarray[i]=0; } return intarray; } -
output : -60 0 0 0
-
required output as : 196 0 0 0
Thanks
Im trying to convert an integer of size 4 to byte array but its
Share
That is because, a (signed) byte is between
-128 to 127If you want to look at how bits are arranged in the memory for each number, just use the
calc.exesupplied with all Windows OS, change theviewtoProgrammerand then, just enter a number and see its bitwise values. That’s what I use all the time for bitwise operations 🙂