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

StringToByteArrayString(byte[] bytes)
To Byte Array String
if (bytes == null) {
    return "null";
StringBuilder sb = new StringBuilder();
String hex = "0123456789ABCDEF";
sb.append("new byte[] { ");
for (int i = 0; i < bytes.length; ++i) {
    if (i > 0) {
...
byte[]toByteArrayV4(String address)
to Byte Array V
String[] tokens = address.split("\\.");
byte[] value = new byte[4];
for (int index = 0; index < 4; index++) {
    value[index] = (byte) Integer.parseInt(tokens[index]);
return value;
byte[]toByteArrayV6(String address)
to Byte Array V
String[] tokens = address.split(":");
byte[] value = new byte[16];
for (int index = 0; index < value.length; index++) {
    value[index] = 0;
boolean emptyGroup = false;
for (int srcIndex = 0, destIndex = 0; destIndex < value.length; srcIndex++, destIndex += 2) {
    if (tokens[srcIndex].length() == 0) {
...
StringtoByteInfo(long bytes)
Converts a byte value into a useful string for output
StringBuilder str = new StringBuilder();
if (bytes < 0) {
    bytes = 0;
str.append(bytes);
str.append(" bytes or ");
str.append(bytes / 1024);
str.append(" kilobytes or ");
...
byte[]toBytes(boolean[] input)
Returns a byte array transformed from the specified boolean array.
byte[] toReturn = new byte[input.length / 8];
for (int entry = 0; entry < toReturn.length; entry++) {
    for (int bit = 0; bit < 8; bit++) {
        if (input[entry * 8 + bit]) {
            toReturn[entry] |= (128 >> bit);
return toReturn;
byte[]toBytes(byte data)
to Bytes
return new byte[] { data };
byte[]toBytes(byte[] b, int offset, long val)
Convert a long value to a byte array using big-endian.
for (int i = offset + 7; i > offset; i--) {
    b[i] = (byte) val;
    val >>>= 8;
b[offset] = (byte) val;
return b;
inttoBytes(byte[] buffer, int pos, String string)
to Bytes
byte[] strBytes = string.getBytes();
System.arraycopy(strBytes, 0, buffer, pos, strBytes.length);
return pos + strBytes.length;
byte[]toBytes(byte[] ipAddress, int port)
to Bytes
if (ipAddress.length != 4)
    throw new IllegalArgumentException("IP address must have 4 parts, not " + ipAddress.length);
byte[] b = new byte[6];
System.arraycopy(ipAddress, 0, b, 0, ipAddress.length);
b[ipAddress.length] = (byte) (port >> 8);
b[ipAddress.length + 1] = (byte) port;
return b;
byte[]toBytes(Byte[] values)
to Bytes
return toBytes(values, DEFAULT_BYTE);