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.
-
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic intbigEndianBytesToInt(byte[] array) static intbigEndianBytesToInt(byte[] array, int offset) static longbigEndianBytesToLong(byte[] array) static longbigEndianBytesToLong(byte[] array, int offset) static shortbigEndianBytesToShort(byte[] array) static shortbigEndianBytesToShort(byte[] array, int offset) 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) static voidintToBEBytes(int v, byte[] output, int offset) static byte[]intToLEBytes(int v) Convert a 32-bit integer to a little-endian encoded array of bytes.static voidintToLEBytes(int v, byte[] output) static voidintToLEBytes(int v, byte[] output, int offset) static intlittleEndianBytesToInt(byte[] array) static intlittleEndianBytesToInt(byte[] array, int offset) static longlittleEndianBytesToLong(byte[] array) static longlittleEndianBytesToLong(byte[] array, int offset) static shortlittleEndianBytesToShort(byte[] array) static shortlittleEndianBytesToShort(byte[] array, int offset) static byte[]longToBEBytes(long v) Convert a 64-bit integer to a big-endian encoded array of bytes.static voidlongToBEBytes(long v, byte[] output) static voidlongToBEBytes(long v, byte[] output, int offset) static byte[]longToLEBytes(long v) Convert a 64-bit integer to a little-endian encoded array of bytes.static voidlongToLEBytes(long v, byte[] output) static voidlongToLEBytes(long v, byte[] output, int offset) 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) static voidshortToBEBytes(short v, byte[] output, int offset) static byte[]shortToLEBytes(short v) Convert a 16-bit integer to a little-endian encoded array of bytes.static voidshortToLEBytes(short v, byte[] output) static voidshortToLEBytes(short v, byte[] output, int offset) 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.
-
Constructor Details
-
EndianUtil
public EndianUtil()
-
-
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-- Returns:
-
swapInt
public static int swapInt(int v) Swap the byte ordering of an int.- Parameters:
v-- Returns:
-
swapLong
public static long swapLong(long v) Swap the byte ordering of a long.- Parameters:
v-- Returns:
-
shortToLEBytes
public static byte[] shortToLEBytes(short v) Convert a 16-bit integer to a little-endian encoded array of bytes.- Parameters:
v-- Returns:
-
shortToLEBytes
public static void shortToLEBytes(short v, byte[] output) -
shortToLEBytes
public static void shortToLEBytes(short v, byte[] output, int offset) -
intToLEBytes
public static byte[] intToLEBytes(int v) Convert a 32-bit integer to a little-endian encoded array of bytes.- Parameters:
v-- Returns:
-
intToLEBytes
public static void intToLEBytes(int v, byte[] output) -
intToLEBytes
public static void intToLEBytes(int v, byte[] output, int offset) -
longToLEBytes
public static byte[] longToLEBytes(long v) Convert a 64-bit integer to a little-endian encoded array of bytes.- Parameters:
v-- Returns:
-
longToLEBytes
public static void longToLEBytes(long v, byte[] output) -
longToLEBytes
public static void longToLEBytes(long v, byte[] output, int offset) -
shortToBEBytes
public static byte[] shortToBEBytes(short v) Convert a 16-bit integer to a big-endian encoded array of bytes.- Parameters:
v-- Returns:
-
shortToBEBytes
public static void shortToBEBytes(short v, byte[] output) -
shortToBEBytes
public static void shortToBEBytes(short v, byte[] output, int offset) -
intToBEBytes
public static byte[] intToBEBytes(int v) Convert a 32-bit integer to a big-endian encoded array of bytes.- Parameters:
v-- Returns:
-
intToBEBytes
public static void intToBEBytes(int v, byte[] output) -
intToBEBytes
public static void intToBEBytes(int v, byte[] output, int offset) -
longToBEBytes
public static byte[] longToBEBytes(long v) Convert a 64-bit integer to a big-endian encoded array of bytes.- Parameters:
v-- Returns:
-
longToBEBytes
public static void longToBEBytes(long v, byte[] output) -
longToBEBytes
public static void longToBEBytes(long v, byte[] output, int offset) -
littleEndianBytesToShort
public static short littleEndianBytesToShort(byte[] array) -
littleEndianBytesToShort
public static short littleEndianBytesToShort(byte[] array, int offset) -
littleEndianBytesToInt
public static int littleEndianBytesToInt(byte[] array) -
littleEndianBytesToInt
public static int littleEndianBytesToInt(byte[] array, int offset) -
littleEndianBytesToLong
public static long littleEndianBytesToLong(byte[] array) -
littleEndianBytesToLong
public static long littleEndianBytesToLong(byte[] array, int offset) -
bigEndianBytesToShort
public static short bigEndianBytesToShort(byte[] array) -
bigEndianBytesToShort
public static short bigEndianBytesToShort(byte[] array, int offset) -
bigEndianBytesToInt
public static int bigEndianBytesToInt(byte[] array) -
bigEndianBytesToInt
public static int bigEndianBytesToInt(byte[] array, int offset) -
bigEndianBytesToLong
public static long bigEndianBytesToLong(byte[] array) -
bigEndianBytesToLong
public static long bigEndianBytesToLong(byte[] array, int offset) -
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- byteOrder 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 -
bytesToNumberUnsigned
Convert a byte array to a number, zero extended to long, meaning that {0x88} will result in 0x88L -
bytesToShort
Read 2 bytes and convert it to a short.- Parameters:
array- input bufferoffset- buffer offsetorder- endianness- Returns:
-
bytesToInt
Read 4 bytes and convert it to an integer.- Parameters:
array- input bufferoffset- buffer offsetorder- endianness- Returns:
-
bytesToLong
Read 8 bytes and convert it to a long.- Parameters:
array- input bufferoffset- buffer offsetorder- endianness- Returns:
-