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[]toBytesFromHexString(String digits)
to Bytes From Hex String
if (digits == null) {
    return null;
int length = digits.length();
if (length % 2 == 1) {
    throw new IllegalArgumentException("For input string: \"" + digits + "\"");
length = length / 2;
...
byte[]toBytesFromOct(String octSymbols)
Given a String of octal digits, return the corresponding byte array
if (octSymbols == null || octSymbols.trim().length() == 0) {
    return (new byte[0]);
} else if (isValidOct(octSymbols) == false) {
    throw new IllegalArgumentException("Invalid octal string specified: " + octSymbols);
while (((octSymbols.length() * BITS_PER_OCT_DIGIT) % 8) != 0) {
    octSymbols = "0" + octSymbols;
int numBytes = (octSymbols.length() * BITS_PER_OCT_DIGIT) / 8;
byte[] bytes = new byte[numBytes];
int byteArrayIndex = 0;
int currentByteIndex = 0;
byte currentByte = 0x00;
byte nextByte = 0x00;
for (int i = 0; i < octSymbols.length(); i++) {
    int octDigit = (Integer.parseInt(octSymbols.substring(i, i + 1))) & 0x07;
    switch (currentByteIndex) {
    case (0):
        currentByte = (byte) (currentByte | (octDigit << 5));
        break;
    case (1):
        currentByte = (byte) (currentByte | (octDigit << 4));
        break;
    case (2):
        currentByte = (byte) (currentByte | (octDigit << 3));
        break;
    case (3):
        currentByte = (byte) (currentByte | (octDigit << 2));
        break;
    case (4):
        currentByte = (byte) (currentByte | (octDigit << 1));
        break;
    case (5):
        currentByte = (byte) (currentByte | octDigit);
        break;
    case (6):
        currentByte = (byte) (currentByte | ((octDigit & 0x06) >>> 1));
        nextByte = (byte) (nextByte | ((octDigit & 0x01) << 7));
        break;
    case (7):
        currentByte = (byte) (currentByte | ((octDigit & 0x04) >>> 2));
        nextByte = (byte) (nextByte | ((octDigit & 0x03) << 6));
        break;
    default:
        return (null);
    if (currentByteIndex > 4) {
        bytes[byteArrayIndex] = currentByte;
        currentByte = nextByte;
        nextByte = 0x00;
        byteArrayIndex++;
    currentByteIndex = (currentByteIndex + BITS_PER_OCT_DIGIT) % 8;
return (bytes);
byte[]toBytesFromString(String s)

Returns a byte array from a string of hexadecimal digits.

int limit = s.length();
byte[] result = new byte[((limit + 1) / 2)];
int i = 0, j = 0;
if ((limit % 2) == 1) {
    result[j++] = (byte) fromDigit(s.charAt(i++));
while (i < limit) {
    result[j++] = (byte) ((fromDigit(s.charAt(i++)) << 4) | fromDigit(s.charAt(i++)));
...
byte[]toBytesFromUnicode(String s)
to Bytes From Unicode
int limit = s.length() * 2;
byte[] result = new byte[limit];
char c;
for (int i = 0; i < limit; i++) {
    c = s.charAt(i >>> 1);
    result[i] = (byte) (((i & 1) == 0) ? c >>> 8 : c);
return result;
...
byte[]toBytesHexEscaped(final byte[] s, final int off, final int len)
to Bytes Hex Escaped
final byte[] out = new byte[(len - 2) / 2];
for (int i = 0; i < out.length; i++) {
    final int j = off + (2 + i * 2);
    byte b1 = hexByte(s[j]);
    byte b2 = hexByte(s[j + 1]);
    out[i] = (byte) ((b1 << 4) | b2);
return out;
...
byte[]toBytesInt32BE(int w)
to Bytes Int BE
byte[] ret = new byte[4];
writeInt32BE(ret, 0, w);
return ret;
byte[]toBytesLittleEndian(long value, int cnt)
Convert a long integer into an array of bytes, little endian format.
byte[] bytes = new byte[cnt];
for (int i = 0; i < cnt; i++) {
    bytes[i] = (byte) (value & 0xff);
    value >>= 8;
return bytes;
byte[]toBytesOctalEscaped(final byte[] s, final int off, final int len)
to Bytes Octal Escaped
final byte[] out;
final int end = off + len;
int correctSize = len;
if (len > MAX_3_BUFF_SIZE) {
    for (int i = off; i < end; ++i) {
        if (s[i] == '\\') {
            byte next = s[++i];
            if (next == '\\') {
...
byte[]toBytesShortBE(short w)
to Bytes Short BE
byte[] ret = new byte[2];
writeShortBE(ret, 0, w);
return ret;
StringtoBytesString(byte[] bytes)
to Bytes String
if (bytes == null) {
    return null;
StringBuilder buffer = new StringBuilder(256);
for (byte b : bytes) {
    buffer.append((char) b);
return buffer.toString();
...