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 int
Limited to Integer.MAX_VALUE: 2^32-1 (4 bytes)
|
static BigInteger |
bytesToBigInteger(byte[] bb) |
static int[] |
bytesToInts(byte[] arr,
boolean bigEndian) |
static void |
bytesToInts(byte[] b,
int[] arr,
boolean bigEndian) |
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
|
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 |
isNullOrZeroArray(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) |
static String |
nibblesToPrettyString(byte[] nibbles)
Turn nibbles to a pretty looking output string
Example.
|
static int |
numBytes(String val)
Calculate the number of bytes need to encode the number
|
static String |
oneByteToHexString(byte value) |
static byte[] |
or(byte[] b1,
byte[] b2) |
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.
Works similar to Hex.toHexString(byte[]) but allows for null |
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[] appendByte(byte[] bytes,
byte b)
bytes - bytes datab - append bytepublic 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[] bigIntegerToBytesSigned(BigInteger b, int numBytes)
public static byte[] bigIntegerToBytes(BigInteger value)
BigIntegers.asUnsignedByteArray(BigInteger)
value - any big integer number. A null-value will return
nullpublic static BigInteger bytesToBigInteger(byte[] bb)
bb - byte[]public 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[] intToBytes(int val)
val - - int value to convertbyte[] of length 4, representing the int valuepublic static byte[] intToBytesNoLeadZeroes(int val)
val - - int value to convertpublic static String toHexString(byte[] data)
Hex.toHexString(byte[]) but allows for nulldata - - byte-array to convert to a hex-stringnullHex.toHexString(byte[])public static byte[] calcPacketLength(byte[] msg)
msg - byte[]public static int byteArrayToInt(byte[] b)
b - array contains the valuespublic static long byteArrayToLong(byte[] b)
b - array contains the valuespublic 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 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-1srcpublic 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)
b1 - b1b2 - b2public static byte[] merge(byte[]... arrays)
arrays - - arrays to mergepublic static boolean isNullOrZeroArray(byte[] array)
public static boolean isSingleZero(byte[] array)
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..'Copyright © 2018. All rights reserved.