Chronicle Bytes: How to write bytes in big endian order on a little endian machine?

147 views Asked by At

I'm using the Bytes<U> interface from here. The underlying buffer is created as follow:

Bytes<?> buf = Bytes.elasticHeapByteBuffer(MAX_SIZE);
buf.writeLong(l);   // writes in little endian

Given my machine byte order is little endian, how can I write the bytes in big endian instead?

Thank you.

Edit: According to this chart, it seems like chronicle bytes does not support this function.

1

There are 1 answers

0
Peter Lawrey On BEST ANSWER

The underlying implementation uses whatever is the native byte order of the CPU. i.e. little-endian on amd64 and ARM, big-endian on Sparc.

You can swap the order with

Bytes<?> buf = Bytes.elasticHeapByteBuffer(MAX_SIZE);
buf.writeLong(Long.reverseLong(l));

long l2 = Long.reverseLong(buf.readLong(l));

Most computers these days are little-endian so it's only an issue when sharing data with a big-endian system.