public class ByteUtil
extends java.lang.Object
| 限定符和类型 | 字段和说明 |
|---|---|
static byte[] |
EMPTY_BYTE_ARRAY |
static byte[] |
ZERO_BYTE_ARRAY |
| 构造器和说明 |
|---|
ByteUtil() |
| 限定符和类型 | 方法和说明 |
|---|---|
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(java.math.BigInteger value)
Omitting sign indication byte.
|
static byte[] |
bigIntegerToBytes(java.math.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(java.math.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 java.math.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(java.math.BigInteger value)
Utility function to copy a byte array into a new byte array with given size.
|
static java.util.Set<byte[]> |
difference(java.util.Set<byte[]> setA,
java.util.Set<byte[]> setB) |
static byte[] |
encodeDataList(java.lang.Object... args)
encode the values and concatenate together
|
static byte[] |
encodeValFor32Bits(java.lang.Object arg) |
static int |
firstNonZeroByte(byte[] data) |
static int |
getBit(byte[] data,
int pos) |
static byte[] |
hexStringToBytes(java.lang.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 ... amount will
never be larger than smallest input
|
static byte[] |
merge(byte[]... arrays) |
static java.lang.String |
nibblesToPrettyString(byte[] nibbles)
Turn nibbles to a pretty looking output string
Example. [ 1, 2, 3, 4, 5 ] becomes '\x11\x23\x45'
|
static int |
numBytes(java.lang.String val)
Calculate the number of bytes need to encode the number
|
static java.lang.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 java.lang.String |
toHexString(byte[] data)
Convert a byte-array into a hex String.
|
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(java.math.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(java.math.BigInteger b,
int numBytes)
public static byte[] bigIntegerToBytes(java.math.BigInteger value)
BigIntegers.asUnsignedByteArray(BigInteger)
value - any big integer number. A null-value will return
nullpublic static java.math.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 java.lang.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 java.lang.String nibblesToPrettyString(byte[] nibbles)
nibbles - - getting byte of data [ 04 ] and turning it to a '\x04'
representationpublic static java.lang.String oneByteToHexString(byte value)
public static int numBytes(java.lang.String val)
val - - numberpublic static byte[] encodeValFor32Bits(java.lang.Object arg)
arg - - not more that 32 bitspublic static byte[] encodeDataList(java.lang.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(java.math.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 java.util.Set<byte[]> difference(java.util.Set<byte[]> setA,
java.util.Set<byte[]> setB)
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(java.lang.String data)
data - String like '0xa5e..' or just 'a5e..'Copyright © 2018. All Rights Reserved.