Package com.pnfsoftware.jeb.util.io
Class EndianUtil
java.lang.Object
com.pnfsoftware.jeb.util.io.EndianUtil
Byte array to/from primitives with endianness support.
-
Method Summary
Modifier and TypeMethodDescriptionstatic intbigEndianBytesToInt(byte[] array) Decode a big-endian 32-bit integer at offset 0.static intbigEndianBytesToInt(byte[] array, int offset) Decode a big-endian 32-bit integer.static longbigEndianBytesToLong(byte[] array) Decode a big-endian 64-bit integer at offset 0.static longbigEndianBytesToLong(byte[] array, int offset) Decode a big-endian 64-bit integer.static shortbigEndianBytesToShort(byte[] array) Decode a big-endian 16-bit integer at offset 0.static shortbigEndianBytesToShort(byte[] array, int offset) Decode a big-endian 16-bit integer.static intbytesToInt(byte[] array, int offset, ByteOrder order) Read 4 bytes and convert it to an integer.static longbytesToLong(byte[] array, int offset, ByteOrder order) Read 8 bytes and convert it to a long.static longbytesToNumberSigned(ByteOrder bo, byte[] b) Convert a byte array to a number, sign is extended to long, meaning that {0x88} will result in 0xFFFFFFFF_FFFFFF88Lstatic longbytesToNumberUnsigned(ByteOrder bo, byte[] b) Convert a byte array to a number, zero extended to long, meaning that {0x88} will result in 0x88Lstatic shortbytesToShort(byte[] array, int offset, ByteOrder order) Read 2 bytes and convert it to a short.static byte[]intToBEBytes(int v) Convert a 32-bit integer to a big-endian encoded array of bytes.static voidintToBEBytes(int v, byte[] output) Encode a 32-bit integer to big-endian bytes.static voidintToBEBytes(int v, byte[] output, int offset) Encode a 32-bit integer to big-endian bytes.static byte[]intToLEBytes(int v) Convert a 32-bit integer to a little-endian encoded array of bytes.static voidintToLEBytes(int v, byte[] output) Encode a 32-bit integer to little-endian bytes.static voidintToLEBytes(int v, byte[] output, int offset) Encode a 32-bit integer to little-endian bytes.static intlittleEndianBytesToInt(byte[] array) Decode a little-endian 32-bit integer at offset 0.static intlittleEndianBytesToInt(byte[] array, int offset) Decode a little-endian 32-bit integer.static longlittleEndianBytesToLong(byte[] array) Decode a little-endian 64-bit integer at offset 0.static longlittleEndianBytesToLong(byte[] array, int offset) Decode a little-endian 64-bit integer.static shortlittleEndianBytesToShort(byte[] array) Decode a little-endian 16-bit integer at offset 0.static shortlittleEndianBytesToShort(byte[] array, int offset) Decode a little-endian 16-bit integer.static byte[]longToBEBytes(long v) Convert a 64-bit integer to a big-endian encoded array of bytes.static voidlongToBEBytes(long v, byte[] output) Encode a 64-bit integer to big-endian bytes.static voidlongToBEBytes(long v, byte[] output, int offset) Encode a 64-bit integer to big-endian bytes.static byte[]longToLEBytes(long v) Convert a 64-bit integer to a little-endian encoded array of bytes.static voidlongToLEBytes(long v, byte[] output) Encode a 64-bit integer to little-endian bytes.static voidlongToLEBytes(long v, byte[] output, int offset) Encode a 64-bit integer to little-endian bytes.static voidnumberToBytes(ByteOrder bo, long v, byte[] output) Automatically convert a value depending on the byte array size.static byte[]shortToBEBytes(short v) Convert a 16-bit integer to a big-endian encoded array of bytes.static voidshortToBEBytes(short v, byte[] output) Encode a 16-bit integer to big-endian bytes.static voidshortToBEBytes(short v, byte[] output, int offset) Encode a 16-bit integer to big-endian bytes.static byte[]shortToLEBytes(short v) Convert a 16-bit integer to a little-endian encoded array of bytes.static voidshortToLEBytes(short v, byte[] output) Encode a 16-bit integer to little-endian bytes.static voidshortToLEBytes(short v, byte[] output, int offset) Encode a 16-bit integer to little-endian bytes.static voidswap(byte[] array) Flip the byte order of an array of bytes.static voidswap(byte[] array, int offset, int size) Flip the byte order of an array of bytes.static voidswapByGroup(byte[] array, int grpByteCount) Flip the byte order of groups of bytes.static intswapInt(int v) Swap the byte ordering of an int.static longswapLong(long v) Swap the byte ordering of a long.static shortswapShort(short v) Swap the byte ordering of a short.
-
Method Details
-
swap
public static void swap(byte[] array, int offset, int size) Flip the byte order of an array of bytes. This method does not create a new array.- Parameters:
array- the input/output arrayoffset- start indexsize- number of elements to flip
-
swap
public static void swap(byte[] array) Flip the byte order of an array of bytes. This method does not create a new array.- Parameters:
array- the input/output array
-
swapByGroup
public static void swapByGroup(byte[] array, int grpByteCount) Flip the byte order of groups of bytes. This method does not create a new array.- Parameters:
array- byte array to changegrpByteCount- number of bytes per group (2 for 16 bits, 4 for 32 bits, 8 for 64 bits)
-
swapShort
public static short swapShort(short v) Swap the byte ordering of a short.- Parameters:
v- input value- Returns:
- byte-swapped value
-
swapInt
public static int swapInt(int v) Swap the byte ordering of an int.- Parameters:
v- input value- Returns:
- byte-swapped value
-
swapLong
public static long swapLong(long v) Swap the byte ordering of a long.- Parameters:
v- input value- Returns:
- byte-swapped value
-
shortToLEBytes
public static byte[] shortToLEBytes(short v) Convert a 16-bit integer to a little-endian encoded array of bytes.- Parameters:
v- input value- Returns:
- two-byte little-endian representation
-
shortToLEBytes
public static void shortToLEBytes(short v, byte[] output) Encode a 16-bit integer to little-endian bytes.- Parameters:
v- input valueoutput- output buffer
-
shortToLEBytes
public static void shortToLEBytes(short v, byte[] output, int offset) Encode a 16-bit integer to little-endian bytes.- Parameters:
v- input valueoutput- output bufferoffset- output offset
-
intToLEBytes
public static byte[] intToLEBytes(int v) Convert a 32-bit integer to a little-endian encoded array of bytes.- Parameters:
v- input value- Returns:
- four-byte little-endian representation
-
intToLEBytes
public static void intToLEBytes(int v, byte[] output) Encode a 32-bit integer to little-endian bytes.- Parameters:
v- input valueoutput- output buffer
-
intToLEBytes
public static void intToLEBytes(int v, byte[] output, int offset) Encode a 32-bit integer to little-endian bytes.- Parameters:
v- input valueoutput- output bufferoffset- output offset
-
longToLEBytes
public static byte[] longToLEBytes(long v) Convert a 64-bit integer to a little-endian encoded array of bytes.- Parameters:
v- input value- Returns:
- eight-byte little-endian representation
-
longToLEBytes
public static void longToLEBytes(long v, byte[] output) Encode a 64-bit integer to little-endian bytes.- Parameters:
v- input valueoutput- output buffer
-
longToLEBytes
public static void longToLEBytes(long v, byte[] output, int offset) Encode a 64-bit integer to little-endian bytes.- Parameters:
v- input valueoutput- output bufferoffset- output offset
-
shortToBEBytes
public static byte[] shortToBEBytes(short v) Convert a 16-bit integer to a big-endian encoded array of bytes.- Parameters:
v- input value- Returns:
- two-byte big-endian representation
-
shortToBEBytes
public static void shortToBEBytes(short v, byte[] output) Encode a 16-bit integer to big-endian bytes.- Parameters:
v- input valueoutput- output buffer
-
shortToBEBytes
public static void shortToBEBytes(short v, byte[] output, int offset) Encode a 16-bit integer to big-endian bytes.- Parameters:
v- input valueoutput- output bufferoffset- output offset
-
intToBEBytes
public static byte[] intToBEBytes(int v) Convert a 32-bit integer to a big-endian encoded array of bytes.- Parameters:
v- input value- Returns:
- four-byte big-endian representation
-
intToBEBytes
public static void intToBEBytes(int v, byte[] output) Encode a 32-bit integer to big-endian bytes.- Parameters:
v- input valueoutput- output buffer
-
intToBEBytes
public static void intToBEBytes(int v, byte[] output, int offset) Encode a 32-bit integer to big-endian bytes.- Parameters:
v- input valueoutput- output bufferoffset- output offset
-
longToBEBytes
public static byte[] longToBEBytes(long v) Convert a 64-bit integer to a big-endian encoded array of bytes.- Parameters:
v- input value- Returns:
- eight-byte big-endian representation
-
longToBEBytes
public static void longToBEBytes(long v, byte[] output) Encode a 64-bit integer to big-endian bytes.- Parameters:
v- input valueoutput- output buffer
-
longToBEBytes
public static void longToBEBytes(long v, byte[] output, int offset) Encode a 64-bit integer to big-endian bytes.- Parameters:
v- input valueoutput- output bufferoffset- output offset
-
littleEndianBytesToShort
public static short littleEndianBytesToShort(byte[] array) Decode a little-endian 16-bit integer at offset 0.- Parameters:
array- input bytes- Returns:
- decoded value
-
littleEndianBytesToShort
public static short littleEndianBytesToShort(byte[] array, int offset) Decode a little-endian 16-bit integer.- Parameters:
array- input bytesoffset- input offset- Returns:
- decoded value
-
littleEndianBytesToInt
public static int littleEndianBytesToInt(byte[] array) Decode a little-endian 32-bit integer at offset 0.- Parameters:
array- input bytes- Returns:
- decoded value
-
littleEndianBytesToInt
public static int littleEndianBytesToInt(byte[] array, int offset) Decode a little-endian 32-bit integer.- Parameters:
array- input bytesoffset- input offset- Returns:
- decoded value
-
littleEndianBytesToLong
public static long littleEndianBytesToLong(byte[] array) Decode a little-endian 64-bit integer at offset 0.- Parameters:
array- input bytes- Returns:
- decoded value
-
littleEndianBytesToLong
public static long littleEndianBytesToLong(byte[] array, int offset) Decode a little-endian 64-bit integer.- Parameters:
array- input bytesoffset- input offset- Returns:
- decoded value
-
bigEndianBytesToShort
public static short bigEndianBytesToShort(byte[] array) Decode a big-endian 16-bit integer at offset 0.- Parameters:
array- input bytes- Returns:
- decoded value
-
bigEndianBytesToShort
public static short bigEndianBytesToShort(byte[] array, int offset) Decode a big-endian 16-bit integer.- Parameters:
array- input bytesoffset- input offset- Returns:
- decoded value
-
bigEndianBytesToInt
public static int bigEndianBytesToInt(byte[] array) Decode a big-endian 32-bit integer at offset 0.- Parameters:
array- input bytes- Returns:
- decoded value
-
bigEndianBytesToInt
public static int bigEndianBytesToInt(byte[] array, int offset) Decode a big-endian 32-bit integer.- Parameters:
array- input bytesoffset- input offset- Returns:
- decoded value
-
bigEndianBytesToLong
public static long bigEndianBytesToLong(byte[] array) Decode a big-endian 64-bit integer at offset 0.- Parameters:
array- input bytes- Returns:
- decoded value
-
bigEndianBytesToLong
public static long bigEndianBytesToLong(byte[] array, int offset) Decode a big-endian 64-bit integer.- Parameters:
array- input bytesoffset- input offset- Returns:
- decoded value
-
numberToBytes
Automatically convert a value depending on the byte array size. If size if not a pow of two, the floor pow of two is filled (for example is size is 5, value will be considered as integer). byte[] of more than 8 bytes will be cropped. This function is a bridge to xxxToLEBytes and xxxToBEBytes functions.- Parameters:
bo- byte order to use for output valuev- input valueoutput- output representation of v, using the given ByteOrder
-
bytesToNumberSigned
Convert a byte array to a number, sign is extended to long, meaning that {0x88} will result in 0xFFFFFFFF_FFFFFF88L- Parameters:
bo- byte order to use for decodingb- input bytes- Returns:
- decoded signed value
-
bytesToNumberUnsigned
Convert a byte array to a number, zero extended to long, meaning that {0x88} will result in 0x88L- Parameters:
bo- byte order to use for decodingb- input bytes- Returns:
- decoded unsigned value
-
bytesToShort
Read 2 bytes and convert it to a short.- Parameters:
array- input bufferoffset- buffer offsetorder- endianness- Returns:
- decoded value
-
bytesToInt
Read 4 bytes and convert it to an integer.- Parameters:
array- input bufferoffset- buffer offsetorder- endianness- Returns:
- decoded value
-
bytesToLong
Read 8 bytes and convert it to a long.- Parameters:
array- input bufferoffset- buffer offsetorder- endianness- Returns:
- decoded value
-