I am trying to switch endianess of ByteBuffer, but there is no effects from it. What am doing wrong? Maybe my debug main function is incorrect?
@Override
public byte[] toBytes(BigDecimal type) {
int octets = getOctetsNumber();
BigInteger intVal = type.unscaledValue();
byte[] temp = intVal.toByteArray();
int addCount = octets - temp.length;
// DEBUG
ByteBuffer buffer = ByteBuffer.allocate(octets);
for(byte b: intVal.toByteArray()){
buffer.put(b);
}
if (addCount > 0){
for (; addCount > 0; addCount--) {
buffer.put((byte)0x00);
}
}
buffer.flip();
buffer.order( ByteOrder.BIG_ENDIAN);
return buffer.array();
}
public static void main(String[] arg) {
IntegerDatatype intVal = new IntegerDatatype(17);
BigDecimal bd = new BigDecimal(32000);
byte[] bytes = intVal.toBytes(bd);
String out = new String();
for (byte b : bytes) {
out += Integer.toBinaryString(b & 255 | 256).substring(1) + " ";
}
System.out.println(out);
}
main function prints this binary string : 01111101 00000000 00000000 00000000 but must prints: 00000000 10111110 00000000 00000000