Class ByteUtils
public final class ByteUtils extends Object
byte
values, the byte-array encoding of larger numbers, as well
as byte arrays in general.-
Method Summary
Modifier and Type Method Description static byte
addMod(int a, int b)
Adds two bytes together with wrap-around behaviour (e.g.static byte[]
array(int... bytes)
Short way to create an array of bytes that also allows 0-255 values.static byte[]
bytes(int value)
Returns a byte array of sizebyteSize(value)
encodingvalue
in big-endian.static int
byteSize(int value)
Returns the minimum number of bytes needed to store the integer.static byte[]
bytesPadded(byte[] bytes, int length)
Pads the given byte array to the givenlength
by adding zeroes at the start.static byte[]
bytesPadded(BigInteger value, int length)
Returns the big-endian encoding ofvalue
in bytes padded (at the start) with zeroes so that the length of the output is at leastlength
.static byte[]
bytesWithoutSign(BigInteger value)
Returns the big-endian encoding ofvalue
in bytes, excluding any byte that would be included solely to signify the sign of the value.static byte[]
concat(byte[]... bytes)
Returns the concatenation of the byte arrays.static byte[]
copyOfSizedRange(byte[] array, int index, int size)
Just likeArrays.copyOfRange(byte[], int, int)
, but allows passing a size instead of an end index.static byte
hexDigitToByte(char hex)
Converts a hex digit (in 0-9, a-f or A-F) to its numeric value.static byte[]
hexStringToBytes(String hexString)
Converts a hex-string (e.g.static byte[]
hexStringToBytes(String hexString, int minLen)
Converts a hex-string (e.g.static char
intToHexDigit(int b)
Converts an integer in [0, 15] into an hex digit (0..9 a..f).static boolean
isByte(int value)
True if the value is in the signed byte range [-128, 127] or if it is in [128, 255] (in which case casting it to a byte yield a negative value which can be used to represent an unsigned byte value).static byte[]
setRangeAt(byte[] dst, int index, byte[] src)
Setsdst[index, index + src.length]
tosrc
.static String
toCompressedHexString(byte[] bytes)
Converts a byte array into a hex string (e.g.static String
toFullHexString(byte[] bytes)
Converts a byte array into a hex string (e.g.static int
toInt(byte... bytes)
Returns the integer encoded by the big-endianbytes
array, whose size should be in[0,4]
.static long
toLong(byte... bytes)
Returns the integer encoded by the big-endianbytes
array, whose size should be in[0,8]
.static byte
uadd(int a, int b)
Samed asaddMod(int, int)
but indicates that the numbers are intepreted as unsigned bytes, and that the addition should not overflow under that interpretation.static int
uint(byte b)
Converts the byte to anint
by reinterpreting its bit pattern as that of an unsigned number.
-
Method Details
-
concat
public static byte[] concat(byte[]... bytes)Returns the concatenation of the byte arrays. -
isByte
public static boolean isByte(int value)True if the value is in the signed byte range [-128, 127] or if it is in [128, 255] (in which case casting it to a byte yield a negative value which can be used to represent an unsigned byte value). -
array
public static byte[] array(int... bytes)Short way to create an array of bytes that also allows 0-255 values. -
uint
public static int uint(byte b)Converts the byte to anint
by reinterpreting its bit pattern as that of an unsigned number. -
addMod
public static byte addMod(int a, int b)Adds two bytes together with wrap-around behaviour (e.g.127 + 1 == -128
).This works whether you want to interpret the byte values as signed or unsigned (where this is equivalent to åddition modulo 255).
In particular, this behaviour is helpful to implement unsigned byte addition: e.g.
127+127
will overflow to-129 == 256 - 127
which is the signed representation of127
.The arguments are supplied as integer, to avoid tiresome casting. Only integer values that can be legaly truncated to a byte value
-
uadd
public static byte uadd(int a, int b)Samed asaddMod(int, int)
but indicates that the numbers are intepreted as unsigned bytes, and that the addition should not overflow under that interpretation. -
byteSize
public static int byteSize(int value)Returns the minimum number of bytes needed to store the integer. This returns 0 if the value is 0. -
bytes
public static byte[] bytes(int value)Returns a byte array of sizebyteSize(value)
encodingvalue
in big-endian. -
toInt
public static int toInt(byte... bytes)Returns the integer encoded by the big-endianbytes
array, whose size should be in[0,4]
.Returns 0 if the byte array is of length 0, making this compatible with RLP-encoding of byte arrays.
-
toLong
public static long toLong(byte... bytes)Returns the integer encoded by the big-endianbytes
array, whose size should be in[0,8]
.Returns 0 if the byte array is of length 0, making this compatible with RLP-encoding of byte arrays.
-
bytesPadded
public static byte[] bytesPadded(byte[] bytes, int length)Pads the given byte array to the givenlength
by adding zeroes at the start.length
should be bigger or equal to the array size. -
bytesPadded
Returns the big-endian encoding ofvalue
in bytes padded (at the start) with zeroes so that the length of the output is at leastlength
.length
should be bigger or equal than the length unpadded encoding ofvalue
, excluding the sign bit.This method does not require a sign bit to be included in the result, and should only be called with positive values.
-
bytesWithoutSign
Returns the big-endian encoding ofvalue
in bytes, excluding any byte that would be included solely to signify the sign of the value.Attention: This means that the 0 big integer encodes as 0-length byte array. This is intended behaviour, as this is how Ethereum encodes such values.
-
setRangeAt
public static byte[] setRangeAt(byte[] dst, int index, byte[] src)Setsdst[index, index + src.length]
tosrc
. -
copyOfSizedRange
public static byte[] copyOfSizedRange(byte[] array, int index, int size)Just likeArrays.copyOfRange(byte[], int, int)
, but allows passing a size instead of an end index. -
hexDigitToByte
public static byte hexDigitToByte(char hex)Converts a hex digit (in 0-9, a-f or A-F) to its numeric value. -
intToHexDigit
public static char intToHexDigit(int b)Converts an integer in [0, 15] into an hex digit (0..9 a..f). -
hexStringToBytes
Converts a hex-string (e.g. "0x123") to a byte array, with the first digits occupying the first array slots. If there is odd number of digit, the first byte matches the first digit (its higher-order nibble (4 bits) will be 0).This accepts the empty hex string ("0x"), for which an empty array is returned.
-
hexStringToBytes
Converts a hex-string (e.g. "0x123") in the same way ashexStringToBytes(String)
, but ensure the returned array will have at least the given minimum length.If the minimum size is larger than the natural size, the low-index bytes are left zeroed.
This accepts the empty hex string ("0x"), for which an empty array is returned.
-
toFullHexString
Converts a byte array into a hex string (e.g. "0x123"). This hex string does display all bytes in the byte array, even if it starts with 0s.This returns "0x" for empty byte arrays.
- See Also:
toCompressedHexString(byte[])
-
toCompressedHexString
Converts a byte array into a hex string (e.g. "0x123"). This hex string does not preserve the length of the byte array if it starts with 0s: the returned string never has leading 0s after the "0x" prefix.This returns "0x" for empty byte arrays, or arrays comprising only 0s.
- See Also:
toFullHexString(byte[])
-