public class ByteUtils
extends java.lang.Object
| Modifier and Type | Field and Description |
|---|---|
static int |
MAX_UNSIGNED_BYTE_AS_INT
Maximum value of unsigned byte as integer.
|
static int |
MIN_UNSIGNED_BYTE_AS_INT
Minimum value of unsigned byte as integer.
|
| Modifier and Type | Method and Description |
|---|---|
static void |
appendHexByte(java.lang.StringBuffer sb,
byte b)
Append the byte to a hexadecimal StringBuilder.
|
static int |
byteArrayToInt(byte[] array,
int offset)
Convert a byte array to an integer.
|
static short |
byteArrayToShort(byte[] array,
int offset)
Convert a byte array to a short.
|
static java.lang.String |
byteArrayToVersion(byte[] array)
Converts the given version in byte array format to string
separated by dots.
|
static boolean |
compareByteArrays(byte[] a,
byte[] a2)
Returns
true if the two specified arrays of bytes are
equal to one another. |
static java.lang.String |
getBits(short n,
int offset,
int length)
Get the first x number of bits of a short.
|
static int |
getMultiByte(byte[] a,
int offset,
int len)
Gets a multi-byte section from a byte array.
|
static byte[] |
intToByteArray(int value)
Converts the given Integer into a byte array.
|
static byte[] |
longToByteArray(long value)
Converts the given Long into a byte array.
|
static byte |
parseHexByte(java.lang.String s)
Converts a hexadecimal String to a byte.
|
static byte[] |
parseHexBytes(java.lang.String s)
Converts a hexadecimal String to a byte array.
|
static void |
setMultiByte(byte[] a,
int offset,
int len,
int value)
Set an integer into a multi-byte section of a byte array.
|
static byte[] |
shortToByteArray(short value)
Converts the given Short into a byte array.
|
static java.lang.String |
toBinaryString(byte n)
Convert byte to a binary string
|
static java.lang.String |
toHexString(byte b)
Converts a byte to a hexadecimal String.
|
static java.lang.String |
toHexString(byte[] data)
Converts a byte array to a hexadecimal String.
|
static java.lang.String |
toHexString(byte[] data,
int offset,
int length,
java.lang.String divider)
Converts a byte array to a hexadecimal String.
|
static java.lang.String |
toReadableString(byte[] data)
Converts a byte array to a readable String.
|
static java.lang.String |
toReadableString(byte[] data,
int offset,
int length)
Converts a byte array to a readable String.
|
static byte[] |
versionToByteArray(java.lang.String version)
Converts the given version string to byte array format.
|
public static final int MAX_UNSIGNED_BYTE_AS_INT
public static final int MIN_UNSIGNED_BYTE_AS_INT
public static void appendHexByte(java.lang.StringBuffer sb,
byte b)
sb - the StringBuilder to append the byte to.b - the byte to append; assumed to be unsigned.public static int byteArrayToInt(byte[] array,
int offset)
array - The byte array to convert.offset - The offset to start converting.public static short byteArrayToShort(byte[] array,
int offset)
array - the byte array to convert.offset - the offset to start converting.public static java.lang.String byteArrayToVersion(byte[] array)
array - byte array to convert to version.public static boolean compareByteArrays(byte[] a,
byte[] a2)
true if the two specified arrays of bytes are
equal to one another. Two arrays are considered equal if both
arrays contain the same number of elements, and all corresponding pairs
of elements in the two arrays are equal. In other words, two arrays are
equal if they contain the same elements in the same order. Also, two
array references are considered equal if both are null.a - one array to be tested for equalitya2 - the other array to be tested for equalitytrue if the two arrays are equalpublic static java.lang.String getBits(short n,
int offset,
int length)
n - The short to get bits from.offset - The offset to start getting bits.length - The number of bits to get.public static int getMultiByte(byte[] a,
int offset,
int len)
a - the byte array to get a multi-byte section of.offset - the offset into the array of the start of the section.len - the length of the section; must be <= 4.java.lang.IllegalArgumentException - if len > 4.java.lang.IndexOutOfBoundsException - if the offset and length
arguments index bytes outside the bounds of the
data array.public static byte[] intToByteArray(int value)
value - Integer value.public static byte[] longToByteArray(long value)
value - Long value.public static byte parseHexByte(java.lang.String s)
s - the String to convert. Must be exactly 2 characters long.java.lang.IllegalArgumentException - if s does not
represent a hexadecimal String.public static byte[] parseHexBytes(java.lang.String s)
s - the String to convert. Must be a multiple of 2 characters long.java.lang.IllegalArgumentException - if s does not
represent a hexadecimal String.public static void setMultiByte(byte[] a,
int offset,
int len,
int value)
a - the byte array to set a multi-byte section of.offset - the offset into the array of the start of the section.len - the length of the section; must be <= 4.value - the value whose low bytes should be set in the array.java.lang.IllegalArgumentException - if len > 4.java.lang.IndexOutOfBoundsException - if the offset and length
arguments index bytes outside the bounds of the
data array.public static byte[] shortToByteArray(short value)
value - Short value.public static java.lang.String toBinaryString(byte n)
n - Byte to convert to binary string.public static java.lang.String toHexString(byte b)
b - the byte to convert; assumed to be unsigned.public static java.lang.String toHexString(byte[] data)
data - the byte array to convert.public static java.lang.String toHexString(byte[] data,
int offset,
int length,
java.lang.String divider)
data - the byte array to convert.offset - the index of the first byte to convert.length - the number of bytes to convert.divider - a String to insert between each byte,
or null if no divider should be inserted.java.lang.IndexOutOfBoundsException - if the offset and length
arguments index bytes outside the bounds of the
data array.public static java.lang.String toReadableString(byte[] data)
data - the byte array to convert.public static java.lang.String toReadableString(byte[] data,
int offset,
int length)
data - the byte array to convert.offset - the index of the first byte to convert.length - the number of bytes to convert.java.lang.IndexOutOfBoundsException - if the offset and length
arguments index bytes outside the bounds of the
data array.public static byte[] versionToByteArray(java.lang.String version)
throws java.lang.NumberFormatException
version - Version string to convert.java.lang.NumberFormatException - if the provided version string does not
follow the format x.x.x.x (where x are
integer values)© Copyright 2014 Etherios, Inc. All rights reserved. Etherios, Inc. is a Division of Digi International Inc.