public class ByteUtil extends Object
Modifier and Type | Field and Description |
---|---|
static byte[] |
EMPTY_BYTE_ARRAY |
static byte[] |
ZERO_BYTE_ARRAY |
Constructor and Description |
---|
ByteUtil() |
Modifier and Type | Method and Description |
---|---|
static byte[] |
and(byte[] b1,
byte[] b2) |
static byte[] |
appendByte(byte[] bytes,
byte b)
Creates a copy of bytes and appends b to the end of it.
|
static short |
bigEndianToShort(byte[] bs) |
static short |
bigEndianToShort(byte[] bs,
int off) |
static byte[] |
bigIntegerToBytes(BigInteger value)
Omitting sign indication byte.
|
static byte[] |
bigIntegerToBytes(BigInteger b,
int numBytes)
The regular
BigInteger.toByteArray() method isn't quite what we often need:
it appends a leading zero to indicate that the number is positive and may need padding. |
static byte[] |
bigIntegerToBytesSigned(BigInteger b,
int numBytes) |
static int |
byteArrayToInt(byte[] b)
Cast hex encoded value from byte[] to int Limited to Integer.MAX_VALUE: 2^32-1 (4 bytes)
|
static long |
byteArrayToLong(byte[] b)
Cast hex encoded value from byte[] to long
null is parsed like byte[0]
Limited to Long.MAX_VALUE: 263-1 (8 bytes)
|
static BigInteger |
bytesToBigInteger(byte[] bb)
Cast hex encoded value from byte[] to BigInteger
null is parsed like byte[0]
|
static int[] |
bytesToInts(byte[] arr,
boolean bigEndian) |
static void |
bytesToInts(byte[] b,
int[] arr,
boolean bigEndian) |
static String |
bytesToIp(byte[] bytesIp)
Converts 4 bytes IPv4 IP to String representation
|
static byte[] |
calcPacketLength(byte[] msg)
Calculate packet length
|
static byte[] |
copyToArray(BigInteger value)
Utility function to copy a byte array into a new byte array with given size.
|
static Set<byte[]> |
difference(Set<byte[]> setA,
Set<byte[]> setB) |
static byte[] |
encodeDataList(Object... args)
encode the values and concatenate together
|
static byte[] |
encodeValFor32Bits(Object arg) |
static int |
firstNonZeroByte(byte[] data) |
static int |
getBit(byte[] data,
int pos) |
static byte[] |
hexStringToBytes(String data)
Converts string hex representation to data bytes
Accepts following hex:
- with or without 0x prefix
- with no leading 0, like 0xabc to 0x0abc
|
static byte[] |
hostToBytes(String ip)
Converts string representation of host/ip to 4-bytes byte[] IPv4
|
static boolean |
increment(byte[] bytes)
increment byte array as a number until max is reached
|
static byte[] |
intsToBytes(int[] arr,
boolean bigEndian) |
static void |
intsToBytes(int[] arr,
byte[] b,
boolean bigEndian) |
static byte[] |
intToBytes(int val)
Converts int value into a byte array.
|
static byte[] |
intToBytesNoLeadZeroes(int val)
Converts a int value into a byte array.
|
static boolean |
isSingleZero(byte[] array) |
static int |
length(byte[]... bytes) |
static byte[] |
longToBytes(long val)
Converts a long value into a byte array.
|
static byte[] |
longToBytesNoLeadZeroes(long val)
Converts a long value into a byte array.
|
static int |
matchingNibbleLength(byte[] a,
byte[] b)
Returns the amount of nibbles that match each other from 0 ...
|
static byte[] |
merge(byte[]... arrays)
merge arrays.
|
static String |
nibblesToPrettyString(byte[] nibbles)
Turn nibbles to a pretty looking output string Example. [ 1, 2, 3, 4, 5 ] becomes
'\x11\x23\x45'
|
static int |
numberOfLeadingZeros(byte[] bytes)
Returns a number of zero bits preceding the highest-order ("leftmost") one-bit
interpreting input array as a big-endian integer value
|
static int |
numBytes(String val)
Calculate the number of bytes need
to encode the number
|
static String |
oneByteToHexString(byte value)
get hex string data from byte data.
|
static byte[] |
or(byte[] b1,
byte[] b2) |
static byte[] |
parseBytes(byte[] input,
int offset,
int len)
Parses fixed number of bytes starting from
offset in input array. |
static byte[] |
parseWord(byte[] input,
int idx)
Parses 32-bytes word from given input.
|
static byte[] |
parseWord(byte[] input,
int offset,
int idx)
Parses 32-bytes word from given input.
|
static byte[] |
setBit(byte[] data,
int pos,
int val) |
static byte[] |
shortToBytes(short n) |
static byte[] |
stripLeadingZeroes(byte[] data) |
static String |
toHexString(byte[] data)
Convert a byte-array into a hex String.
|
static ByteArrayWrapper |
wrap(byte[] data) |
static byte[] |
xor(byte[] b1,
byte[] b2) |
static byte[] |
xorAlignRight(byte[] b1,
byte[] b2)
XORs byte arrays of different lengths by aligning length of the shortest via adding zeros at beginning
|
public static final byte[] EMPTY_BYTE_ARRAY
public static final byte[] ZERO_BYTE_ARRAY
public static byte[] bigIntegerToBytes(BigInteger b, int numBytes)
BigInteger.toByteArray()
method isn't quite what we often need:
it appends a leading zero to indicate that the number is positive and may need padding.b
- the integer to format into a byte arraynumBytes
- the desired size of the resulting byte arraypublic static byte[] bigIntegerToBytes(BigInteger value)
BigIntegers.asUnsignedByteArray(BigInteger)
value
- - any big integer number. A null
-value will return null
public static byte[] merge(byte[]... arrays)
arrays
- - arrays to mergepublic static byte[] appendByte(byte[] bytes, byte b)
public static String nibblesToPrettyString(byte[] nibbles)
nibbles
- - getting byte of data [ 04 ] and turning it to a '\x04' representationpublic static String oneByteToHexString(byte value)
public static String toHexString(byte[] data)
Hex.toHexString(byte[])
but allows
for null
data
- - byte-array to convert to a hex-stringnull
Hex.toHexString(byte[])
public static int byteArrayToInt(byte[] b)
b
- array contains the valuespublic static boolean isSingleZero(byte[] array)
public static byte[] intToBytesNoLeadZeroes(int val)
val
- - int value to convertpublic static byte[] intToBytes(int val)
val
- - int value to convertbyte[]
of length 4, representing the int valuepublic static byte[] bigIntegerToBytesSigned(BigInteger b, int numBytes)
public static BigInteger bytesToBigInteger(byte[] bb)
bb
- byte array contains the valuespublic static int matchingNibbleLength(byte[] a, byte[] b)
a
- - first inputb
- - second inputpublic static byte[] longToBytes(long val)
val
- - long value to convertbyte[]
of length 8, representing the long valuepublic static byte[] longToBytesNoLeadZeroes(long val)
val
- - long value to convertpublic static byte[] calcPacketLength(byte[] msg)
msg
- byte[]public static long byteArrayToLong(byte[] b)
b
- array contains the valuespublic static int numBytes(String val)
val
- - numberpublic static byte[] encodeValFor32Bits(Object arg)
arg
- - not more that 32 bitspublic static byte[] encodeDataList(Object... args)
args
- Objectpublic static int firstNonZeroByte(byte[] data)
public static byte[] stripLeadingZeroes(byte[] data)
public static boolean increment(byte[] bytes)
bytes
- byte[]public static byte[] copyToArray(BigInteger value)
value
- - a BigInteger with a maximum value of 2^256-1src
public static ByteArrayWrapper wrap(byte[] data)
public static byte[] setBit(byte[] data, int pos, int val)
public static int getBit(byte[] data, int pos)
public static byte[] and(byte[] b1, byte[] b2)
public static byte[] or(byte[] b1, byte[] b2)
public static byte[] xor(byte[] b1, byte[] b2)
public static byte[] xorAlignRight(byte[] b1, byte[] b2)
public static int length(byte[]... bytes)
public static byte[] intsToBytes(int[] arr, boolean bigEndian)
public static int[] bytesToInts(byte[] arr, boolean bigEndian)
public static void bytesToInts(byte[] b, int[] arr, boolean bigEndian)
public static void intsToBytes(int[] arr, byte[] b, boolean bigEndian)
public static short bigEndianToShort(byte[] bs)
public static short bigEndianToShort(byte[] bs, int off)
public static byte[] shortToBytes(short n)
public static byte[] hexStringToBytes(String data)
data
- String like '0xa5e..' or just 'a5e..'public static byte[] hostToBytes(String ip)
public static String bytesToIp(byte[] bytesIp)
public static int numberOfLeadingZeros(byte[] bytes)
public static byte[] parseBytes(byte[] input, int offset, int len)
offset
in input
array.
If input
has not enough bytes return array will be right padded with zero bytes.
I.e. if offset
is higher than input.length
then zero byte array of length len
will be returnedpublic static byte[] parseWord(byte[] input, int idx)
parseBytes(byte[], int, int)
method,
thus, result will be right-padded with zero bytes if there is not enough bytes in input
idx
- an index of the word starting from 0
public static byte[] parseWord(byte[] input, int offset, int idx)
parseBytes(byte[], int, int)
method,
thus, result will be right-padded with zero bytes if there is not enough bytes in input
idx
- an index of the word starting from 0
offset
- an offset in input
array to start parsing fromCopyright © 2020. All rights reserved.