1

I would like to ask if there is already a standard Java library that implements the following two things:

  1. converting between endlessness standard Java primitive types + UUID.
  2. lossless conversion between signed and unsigned standard Java primitive types. By lossless I mean for example that converting from signed short to unsigned short then the result could be signed Integer.

If such exists then what is the name of that library.

Benedikt Köppel
  • 4,853
  • 4
  • 32
  • 42
Tito
  • 2,234
  • 6
  • 31
  • 65

1 Answers1

1

You could use the ByteSwapper from here: http://www.java2s.com/Code/Java/Language-Basics/Utilityforbyteswappingofalljavadatatypes.htm.

Or alternatively, use the ByteBuffer and specify the endianness with order(), as explained here: https://stackoverflow.com/a/14827440/1067124 (copied below)

With java.nio.ByteBuffer you may specify the endianness you want: order().

ByteBuffer have methods to extract data as byte, char, getShort(), getInt(), long, double...

Here's an example how to use it:

ByteBuffer bb = ByteBuffer.wrap(byteArray);
bb.order( ByteOrder.LITTLE_ENDIAN);
while( bb.hasRemaining()) {
   short v = bb.getShort();
   /* Do something with v... */
}
Community
  • 1
  • 1
Benedikt Köppel
  • 4,853
  • 4
  • 32
  • 42