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[]toBytes(char c)
Converts a single (unicode) char to a byte-array.
return new byte[] { (byte) (c & 0xff), (byte) ((c >> 8) & 0xff) };
byte[]toBytes(char value)
to Bytes
byte[] bt = new byte[2];
for (int i = 0; i < bt.length; i++) {
    bt[i] = (byte) (value >>> i * 8);
return bt;
voidtoBytes(char[] cbuf, byte[] bs)
to Bytes
for (int i = 0, j = 0; i < bs.length; i++, j++, j++) {
    bs[i] = (byte) ((hex2digit(cbuf[j]) << 4) | hex2digit(cbuf[j + 1]));
byte[]toBytes(char[] chars)
to Bytes
byte[] ret = new byte[chars.length];
for (int i = 0; i < chars.length; i++) {
    ret[i] = (byte) chars[i];
return ret;
byte[]toBytes(final byte[] s, final int off, final int len)
to Bytes
if (s.length < 2 || s[0] != '\\' || s[1] != 'x') {
    return toBytesOctalEscaped(s, off, len);
return toBytesHexEscaped(s, off, len);
byte[]toBytes(final long n)
Returns a 8-byte array built from a long.
return toBytes(n, new byte[8]);
byte[]toBytes(final long val)
Converts a 32 bit value to an array of bytes.
byte[] bytes = new byte[4];
bytes[3] = (byte) (val & 0xFF);
bytes[2] = (byte) ((val & 0xFF00) >>> 8);
bytes[1] = (byte) ((val & 0xFF0000) >>> 16);
bytes[0] = (byte) ((val & 0xFF000000) >>> 32);
return bytes;
byte[]toBytes(final String hexaString)
Convert a base 16 string into bytes.
if (hexaString.length() % 2 == 1) {
    throw new IllegalArgumentException("String length must be even."); 
final byte[] bytes = new byte[hexaString.length() / 2];
for (int bi = 0, i = 0; bi < bytes.length; bi++) {
    int b = Character.digit(hexaString.charAt(i++), 16) << 4;
    b |= Character.digit(hexaString.charAt(i++), 16);
    bytes[bi] = (byte) b;
...
byte[]toBytes(final String str)
Converts a hex-encoded string to bytes.
final int len = str.length();
if (len % 2 != 0) {
    throw new IllegalArgumentException("String '" + str + "'.length() % 2 != 0");
final byte[] buf = new byte[len / 2];
int idx = 0;
for (int i = 0; i < buf.length; i++) {
    final int b1 = charToOctet(str.charAt(idx++));
...
byte[]toBytes(final String text)
Convert String into byte array
final byte[] bytes = new byte[text.length() + 1];
System.arraycopy(text.getBytes(), 0, bytes, 0, text.length());
bytes[text.length()] = '\0';
return bytes;