Java Utililty Methods Byte Array Create

List of utility methods to do Byte Array Create

Description

The list of methods to do Byte Array Create are organized into topic(s).

Method

byte[]toBytes4(int n)
to Bytes
byte[] b = new byte[4];
b[0] = (byte) (n);
n >>>= 8;
b[1] = (byte) (n);
n >>>= 8;
b[2] = (byte) (n);
n >>>= 8;
b[3] = (byte) (n);
...
byte[]toBytes4HexString(String str, char... separateds)
to Bytes Hex String
if (str == null)
    return new byte[0];
char separated = ' ';
if (separateds != null && separateds.length == 1) {
    separated = separateds[0];
    String[] strArr = str.split(separated + "");
    byte[] array = new byte[strArr.length];
    for (int i = 0; i < strArr.length; i++) {
...
byte[]toBytesBE(long v)
to Bytes BE
return new byte[] { (byte) (v >>> 56), (byte) (v >>> 48), (byte) (v >>> 40), (byte) (v >>> 32),
        (byte) (v >>> 24), (byte) (v >>> 16), (byte) (v >>> 8), (byte) (v >>> 0), };
byte[]toBytesBigEndian(long value, int sizeInByte)
Convert int/long to n-byte array.
byte[] out = new byte[sizeInByte];
for (int i = (sizeInByte - 1); i >= 0; i--) {
    out[i] = (byte) value;
    value >>>= 8;
return out;
byte[]toBytesBinary(String in)
to Bytes Binary
byte[] b = new byte[in.length()];
int size = 0;
for (int i = 0; i < in.length(); ++i) {
    char ch = in.charAt(i);
    if (ch == '\\' && in.length() > i + 1 && in.charAt(i + 1) == 'x') {
        char hd1 = in.charAt(i + 2);
        char hd2 = in.charAt(i + 3);
        if (!isHexDigit(hd1) || !isHexDigit(hd2)) {
...
byte[]toBytesDirect(final String singleOctets)
Returns a byte array representing the given string, truncating each character into a byte directly.
final char[] src = singleOctets.toCharArray();
final byte[] dest = new byte[src.length];
for (int i = 0; i < dest.length; i++) {
    final char c = src[i];
    if (c > Byte.MAX_VALUE)
        throw new IllegalArgumentException(
                "Invalid character found at position " + i + " for " + singleOctets);
    dest[i] = (byte) c;
...
byte[]toBytesFromASCII(final char[] chars)
to Bytes From ASCII
final byte[] b = new byte[chars.length];
for (int i = 0; i < b.length; i++) {
    b[i] = (byte) chars[i];
return b;
byte[]toBytesFromBase64(String inBase64String)
to Bytes From Base
if (inBase64String == null) {
    return null;
if (inBase64String.trim().isEmpty()) {
    return EMPTY_BYTES;
return Base64.getDecoder().decode(inBase64String);
byte[]toBytesFromBin(String binSymbols)
Given a string of binary symbols, convert it to an array of bytes
if (binSymbols == null || binSymbols.trim().length() == 0) {
    return (new byte[0]);
} else if (isValidBin(binSymbols) == false) {
    throw new IllegalArgumentException("Invalid bin string specified");
binSymbols = stripBinaryPrefix(binSymbols);
while ((binSymbols.length() % 8) != 0) {
    binSymbols = "0" + binSymbols;
...
byte[]toBytesFromHexStr(String hexStr)
to Bytes From Hex Str
int len = hexStr.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i = i + 2) {
    data[i / 2] = (byte) ((Character.digit(hexStr.charAt(i), 16) << 4)
            + Character.digit(hexStr.charAt(i + 1), 16));
return data;