Class ByteUtils
Some routines have been adapted from Apache Commons Codec's Hex.java class.
- Since:
- 1.54
- Version:
- 1.3
- Author:
- matt
-
Field Summary
FieldsModifier and TypeFieldDescriptionstatic final CharsetThe ASCII character set.static final StringThe ASCII character set name.static final CharsetThe ISO-8859-1 (ISO-LATIN-1) character set.static final StringThe ISO-8859-1 (ISO-LATIN-1) character set name.static final CharsetThe UTF-8 character set.static final StringThe UTF-8 character set name. -
Constructor Summary
Constructors -
Method Summary
Modifier and TypeMethodDescriptionstatic byte[]Convert an array of bytes to Byte objects.static byte[]Convert an array of bytes to Byte objects.static byte[]decodeHexPadStart(char[] chars) Convert a hex-encoded string to a byte array.static byte[]Convert a hex-encoded string to a byte array.static char[]encodeHex(byte b, char[] toDigits, char[] dest, int destIndex) Encode a single byte as hex characters.static char[]encodeHexLowerCase(byte b, char[] dest, int destIndex) Encode a single byte as lower-case hex characters.static StringencodeHexString(byte[] data, int fromIndex, int toIndex, boolean space) Encode a byte array into a hex-encoded upper-case string.static StringencodeHexString(byte[] data, int fromIndex, int toIndex, boolean space, boolean lowerCase) Encode a byte array into a hex-encoded upper-case string.static char[]encodeHexUpperCase(byte b, char[] dest, int destIndex) Encode a single byte as upper-case hex characters.static voidencodeInt16(Number n, byte[] dest, int offset, ByteOrdering byteOrder) Encode a 16-bit signed integer value into a raw byte value.static voidencodeInt32(Number n, byte[] dest, int offset, ByteOrdering byteOrder) Encode a 32-bit signed integer value into a raw byte value.static voidencodeInt64(long l, byte[] dest, int offset, ByteOrdering byteOrder) Encode a 64-bit signed integer value into a raw byte value.static voidencodeInt64(Number n, byte[] dest, int offset, ByteOrdering byteOrder) Encode a 64-bit signed integer value into a raw byte value.static voidencodeInt8(Number n, byte[] dest, int offset) Encode an 8-bit signed integer value into a raw byte value.static voidencodeUnsignedInt16(Number n, byte[] dest, int offset, ByteOrdering byteOrder) Encode a 16-bit unsigned integer value into a raw byte value.static voidencodeUnsignedInt32(Number n, byte[] dest, int offset, ByteOrdering byteOrder) Encode a 32-bit unsigned integer value into a raw byte value.static voidencodeUnsignedInt64(long l, byte[] dest, int offset, ByteOrdering byteOrder) Encode a 64-bit unsigned integer value into a raw byte value.static voidencodeUnsignedInt64(Number n, byte[] dest, int offset, ByteOrdering byteOrder) Encode a 64-bit unsigned integer value into a raw byte value.static voidencodeUnsignedInt8(Number n, byte[] dest, int offset) Encode an 8-bit unsigned integer value into a raw byte value.static StringhumanReadableByteCount(long count) Format a byte count in a "human readable" format, with progressively larger binary units (powers of 1024).static Byte[]objectArray(byte[] array) Convert an array of bytes to Byte objects.static byte[]parseBytes(byte[] data, int offset, int length, ByteOrdering byteOrder) Parse any number of byte values as a series of bytes.static BigDecimalparseDecimalCharacterString(byte[] data, int offset, int length, ByteOrdering byteOrder, Charset charset) Parse any number of byte values representing characters into a decimal string.static FloatparseFloat32(byte d, byte c, byte b, byte a) Parse an IEEE-754 32-bit float value from raw byte values.static DoubleparseFloat64(byte h, byte g, byte f, byte e, byte d, byte c, byte b, byte a) Parse an IEEE-754 64-bit floating point value from raw byte values.static ShortparseInt16(byte hi, byte lo) Parse a 16-bit signed integer value from a raw byte value.static IntegerparseInt32(byte d, byte c, byte b, byte a) Parse a 32-bit signed integer value from raw byte values.static LongparseInt64(byte h, byte g, byte f, byte e, byte d, byte c, byte b, byte a) Parse a 64-bit signed integer value from raw byte values.static ByteparseInt8(byte b) Parse an 8-bit signed integer value from a raw byte value.static BigIntegerparseInteger(byte[] data, int offset, int length, ByteOrdering byteOrder) Parse any number of byte values into an signedBigInteger.static NumberparseNumber(BitDataType dataType, byte[] data, int offset, int length, ByteOrdering byteOrder) Parse a number from raw byte data.static NumberparseNumber(BitDataType dataType, byte[] data, int offset, ByteOrdering byteOrder) Parse a number from raw byte data.static IntegerparseUnsignedInt16(byte hi, byte lo) Parse a 16-bit unsigned integer value from a raw byte value.static LongparseUnsignedInt32(byte d, byte c, byte b, byte a) Parse a 32-bit unsigned integer value from raw byte values.static BigIntegerparseUnsignedInt64(byte h, byte g, byte f, byte e, byte d, byte c, byte b, byte a) Construct an 64-bit unsigned integer from raw byte values.static ShortparseUnsignedInt8(byte b) Parse an 8-bit unsigned integer value from a raw byte value.static BigIntegerparseUnsignedInteger(byte[] data, int offset, int length, ByteOrdering byteOrder) Parse any number of byte values into an unsignedBigInteger.
-
Field Details
-
UTF8_CHARSET
The UTF-8 character set name.- See Also:
-
UTF8
The UTF-8 character set. -
ASCII_CHARSET
The ASCII character set name.- See Also:
-
ASCII
The ASCII character set. -
LATIN1_CHARSET
The ISO-8859-1 (ISO-LATIN-1) character set name.- Since:
- 1.1
- See Also:
-
LATIN1
The ISO-8859-1 (ISO-LATIN-1) character set.- Since:
- 1.1
-
-
Constructor Details
-
ByteUtils
public ByteUtils()
-
-
Method Details
-
encodeHex
public static char[] encodeHex(byte b, char[] toDigits, char[] dest, int destIndex) Encode a single byte as hex characters.- Parameters:
b- the byte to encodetoDigits- the hex alphabet to usedest- the destination character buffer to write the hex encoding todestIndex- the index withindestto write the hex encoding at, along withdestIndex + 1- Returns:
- the
destarray
-
encodeHexUpperCase
public static char[] encodeHexUpperCase(byte b, char[] dest, int destIndex) Encode a single byte as upper-case hex characters.- Parameters:
b- the byte to encodedest- the destination character buffer to write the hex encoding todestIndex- the index withindestto write the hex encoding at, along withdestIndex + 1- Returns:
- the
destarray
-
encodeHexLowerCase
public static char[] encodeHexLowerCase(byte b, char[] dest, int destIndex) Encode a single byte as lower-case hex characters.- Parameters:
b- the byte to encodedest- the destination character buffer to write the hex encoding todestIndex- the index withindestto write the hex encoding at, along withdestIndex + 1- Returns:
- the
destarray
-
encodeHexString
Encode a byte array into a hex-encoded upper-case string.- Parameters:
data- the data to encode as hex stringsfromIndex- the starting index withindatato encode (inclusive)toIndex- the ending index withindatato encode (exclusive)space- true to add a single space character between each hex pair- Returns:
- the string, never null
-
encodeHexString
public static String encodeHexString(byte[] data, int fromIndex, int toIndex, boolean space, boolean lowerCase) Encode a byte array into a hex-encoded upper-case string.- Parameters:
data- the data to encode as hex stringsfromIndex- the starting index withindatato encode (inclusive)toIndex- the ending index withindatato encode (exclusive)space- true to add a single space character between each hexlowerCase- true to use lower case, false for upper case pair- Returns:
- the string, never null
-
decodeHexString
Convert a hex-encoded string to a byte array.If the string does not have an even number of characters, a 0 will be inserted at the start of the string.
- Parameters:
s- the string to decode- Returns:
- the bytes, never null
- See Also:
-
decodeHexPadStart
public static byte[] decodeHexPadStart(char[] chars) Convert a hex-encoded string to a byte array.If the string does not have an even number of characters, a 0 will be inserted at the start of the string.
- Parameters:
chars- the characters to decode- Returns:
- the bytes, never null
-
encodeInt8
Encode an 8-bit signed integer value into a raw byte value.- Parameters:
n- the number to encodedest- the destination to encode the number tooffset- the offset withindestto encode the number to- Throws:
ArrayIndexOutOfBoundsException- ifdestis not long enough to hold the number's byte value
-
encodeUnsignedInt8
Encode an 8-bit unsigned integer value into a raw byte value.- Parameters:
n- the number to encodedest- the destination to encode the number tooffset- the offset withindestto encode the number to- Throws:
ArrayIndexOutOfBoundsException- ifdestis not long enough to hold the number's byte value
-
encodeInt16
Encode a 16-bit signed integer value into a raw byte value.- Parameters:
n- the number to encodedest- the destination to encode the number tooffset- the offset withindestto encode the number tobyteOrder- the byte order to encode intodest- Throws:
ArrayIndexOutOfBoundsException- ifdestis not long enough to hold the number's byte value
-
encodeUnsignedInt16
Encode a 16-bit unsigned integer value into a raw byte value.- Parameters:
n- the number to encodedest- the destination to encode the number tooffset- the offset withindestto encode the number tobyteOrder- the byte order to encode intodest- Throws:
ArrayIndexOutOfBoundsException- ifdestis not long enough to hold the number's byte value
-
encodeInt32
Encode a 32-bit signed integer value into a raw byte value.- Parameters:
n- the number to encodedest- the destination to encode the number tooffset- the offset withindestto encode the number tobyteOrder- the byte order to encode intodest- Throws:
ArrayIndexOutOfBoundsException- ifdestis not long enough to hold the number's byte value
-
encodeUnsignedInt32
Encode a 32-bit unsigned integer value into a raw byte value.- Parameters:
n- the number to encodedest- the destination to encode the number tooffset- the offset withindestto encode the number tobyteOrder- the byte order to encode intodest- Throws:
ArrayIndexOutOfBoundsException- ifdestis not long enough to hold the number's byte value
-
encodeInt64
Encode a 64-bit signed integer value into a raw byte value.- Parameters:
n- the number to encodedest- the destination to encode the number tooffset- the offset withindestto encode the number tobyteOrder- the byte order to encode intodest- Throws:
ArrayIndexOutOfBoundsException- ifdestis not long enough to hold the number's byte value
-
encodeInt64
Encode a 64-bit signed integer value into a raw byte value.- Parameters:
l- the number to encodedest- the destination to encode the number tooffset- the offset withindestto encode the number tobyteOrder- the byte order to encode intodest- Throws:
ArrayIndexOutOfBoundsException- ifdestis not long enough to hold the number's byte value
-
encodeUnsignedInt64
Encode a 64-bit unsigned integer value into a raw byte value.- Parameters:
n- the number to encodedest- the destination to encode the number tooffset- the offset withindestto encode the number tobyteOrder- the byte order to encode intodest- Throws:
ArrayIndexOutOfBoundsException- ifdestis not long enough to hold the number's byte value
-
encodeUnsignedInt64
Encode a 64-bit unsigned integer value into a raw byte value.- Parameters:
l- the number to encodedest- the destination to encode the number tooffset- the offset withindestto encode the number tobyteOrder- the byte order to encode intodest- Throws:
ArrayIndexOutOfBoundsException- ifdestis not long enough to hold the number's byte value
-
parseNumber
public static Number parseNumber(BitDataType dataType, byte[] data, int offset, ByteOrdering byteOrder) Parse a number from raw byte data.This method is suitable for fixed-length data types only.
- Parameters:
dataType- the desired data typedata- an array of byte valuesoffset- an offset withindatato start reading frombyteOrder- the byte order ofdata- Returns:
- the parsed number, or null if
datais null or not long enough for the requested data type - Throws:
IllegalArgumentException- ifdataTypeis not supported
-
parseNumber
public static Number parseNumber(BitDataType dataType, byte[] data, int offset, int length, ByteOrdering byteOrder) Parse a number from raw byte data.- Parameters:
dataType- the desired data typedata- an array of byte valuesoffset- an offset withindatato start reading fromlength- for types of variable length, the number of bytes to consume; for types of fixed length this value is ignoredbyteOrder- the byte order ofdata- Returns:
- the parsed number, or null if
datais null or not long enough for the requested data type - Throws:
IllegalArgumentException- ifdataTypeis not supported
-
parseInt8
Parse an 8-bit signed integer value from a raw byte value.- Parameters:
b- bits 7-0- Returns:
- the parsed integer, never null
-
parseUnsignedInt8
Parse an 8-bit unsigned integer value from a raw byte value.Note a
Shortis returned to support unsigned 8-bit values.- Parameters:
b- bits 7-0- Returns:
- the parsed integer, never null
-
parseInt16
Parse a 16-bit signed integer value from a raw byte value.- Parameters:
hi- bits 15-8lo- bits 7-0- Returns:
- the parsed integer, never null
-
parseUnsignedInt16
Parse a 16-bit unsigned integer value from a raw byte value.Note a
Integeris returned to support unsigned 16-bit values.- Parameters:
hi- bits 15-8lo- bits 7-0- Returns:
- the parsed integer, never null
-
parseInt32
Parse a 32-bit signed integer value from raw byte values.- Parameters:
d- bits 31-24c- bits 23-16b- bits 15-8a- bits 7-0- Returns:
- the parsed integer, never null
-
parseUnsignedInt32
Parse a 32-bit unsigned integer value from raw byte values.Note a
Longis returned to support unsigned 32-bit values.- Parameters:
d- bits 31-24c- bits 23-16b- bits 15-8a- bits 7-0- Returns:
- the parsed integer, never null
-
parseInt64
Parse a 64-bit signed integer value from raw byte values.- Parameters:
h- bits 63-56g- bits 55-48f- bits 47-40e- bits 39-32d- bits 31-24c- bits 23-16b- bits 15-8a- bits 7-0- Returns:
- the parsed integer, never null
-
parseUnsignedInt64
public static BigInteger parseUnsignedInt64(byte h, byte g, byte f, byte e, byte d, byte c, byte b, byte a) Construct an 64-bit unsigned integer from raw byte values.Note a
BigIntegeris returned to support unsigned 64-bit values.- Parameters:
h- bits 63-56g- bits 55-48f- bits 47-40e- bits 39-32d- bits 31-24c- bits 23-16b- bits 15-8a- bits 7-0- Returns:
- the parsed integer, never null
-
parseFloat32
Parse an IEEE-754 32-bit float value from raw byte values.- Parameters:
d- bits 31-24c- bits 23-16b- bits 15-8a- bits 7-0- Returns:
- the parsed float, or null if not available or parsed
float is
NaN
-
parseFloat64
Parse an IEEE-754 64-bit floating point value from raw byte values.- Parameters:
h- bits 63-56g- bits 55-48f- bits 47-40e- bits 39-32d- bits 31-24c- bits 23-16b- bits 15-8a- bits 7-0- Returns:
- the parsed float, or null if the result is
NaN
-
parseBytes
Parse any number of byte values as a series of bytes.- Parameters:
data- the dataoffset- the byte offset to start fromlength- the number of bytes to consumebyteOrder- the byte order ofdata- Returns:
- the parsed bytes, never null
-
parseInteger
Parse any number of byte values into an signedBigInteger.- Parameters:
data- the data to parseoffset- the offset withindatato start reading fromlength- the number of bytes to consumebyteOrder- the byte order ofdata- Returns:
- the integer value
-
parseUnsignedInteger
public static BigInteger parseUnsignedInteger(byte[] data, int offset, int length, ByteOrdering byteOrder) Parse any number of byte values into an unsignedBigInteger.- Parameters:
data- the data to parseoffset- the offset withindatato start reading fromlength- the number of bytes to consumebyteOrder- the byte order ofdata- Returns:
- the integer value
-
parseDecimalCharacterString
public static BigDecimal parseDecimalCharacterString(byte[] data, int offset, int length, ByteOrdering byteOrder, Charset charset) Parse any number of byte values representing characters into a decimal string.This method interprets the
databytes as a string encoded ascharset, whose string value contains a string decimal number. For example, a big-endian ASCII encoded byte array of 0x31 0x30 0x32 0x34 is interpreted as the string "1024" which is then parsed as aBigDecimal.- Parameters:
data- the data to parseoffset- the offset withindatato start reading fromlength- the number of bytes to consumebyteOrder- the byte order ofdatacharset- the character set to interpret the bytes as- Returns:
- the number
-
objectArray
Convert an array of bytes to Byte objects.- Parameters:
array- the array to convert- Returns:
- the converted array, or null if
arrayis null - Since:
- 1.1
-
byteArray
Convert an array of bytes to Byte objects.0 will be used for any null object values.
- Parameters:
array- the array to convert- Returns:
- the converted array, or null if
arrayis null - Since:
- 1.1
-
byteArray
Convert an array of bytes to Byte objects.- Parameters:
array- the array to convertnullValue- the byte value to use for null Byte values- Returns:
- the converted array, or null if
arrayis null - Since:
- 1.1
-
humanReadableByteCount
Format a byte count in a "human readable" format, with progressively larger binary units (powers of 1024).For example this method will return 6.8 MiB for input of 7077888.
Adapted from @aioobe on Stack Overflow
- Parameters:
count- the byte count to format- Returns:
- the count formatted as a string
- Since:
- 1.3
-