0

I have a decimal number and I would convert it in an array buffer of byte (little endian notation).

I try this but I am not sure it is working:

ByteBuffer a = ByteBuffer.allocate(4);
      a.putInt( (int) number);
      return a.array();

If number is 125 I have returned:

[0,0,0,128];

Is it correct? I think that the correct conversion would be:

[0,0,0,10000000]

How can I do with Java?

Thanks in advance

michele
  • 26,348
  • 30
  • 111
  • 168

1 Answers1

0

1) from ByteBuffer API

The initial order of a byte buffer is always BIG_ENDIAN. 

so, you have to change the byte buffer order to little endian in the first place

2) there is only one representation of decimal number in Java, it is BigDecimal, and cannot cast it to int. Thus, this cannot work

 a.putInt( (int) number);
Evgeniy Dorofeev
  • 133,369
  • 30
  • 199
  • 275