Java Utililty Methods Binary Encode

List of utility methods to do Binary Encode

Description

The list of methods to do Binary Encode are organized into topic(s).

Method

StringtoBinary(long l, int bits)
to Binary
StringBuilder buffer = new StringBuilder(bits);
for (int i = 0; i < bits; i++) {
    buffer.insert(0, l & 1);
    l >>>= 1;
return buffer.toString();
char[]toBinary(long v, int len)
to Binary
char[] cs = new char[len];
for (int i = 0; i < len; i++) {
    cs[i] = (v >> (len - i - 1) & 1) != 0 ? '1' : '0';
return cs;
StringtoBinary(short value)
to Binary
return String.format("%16s", Integer.toString(value & 0xFFFF, 2)).replace(' ', '0');
StringtoBinaryAddress(int index, int maxIndex)
Returns index as a binary string.
int digits = Math.max(32 - Integer.numberOfLeadingZeros(maxIndex), 1);
char[] buffer = new char[digits];
int mask = 1;
for (int i = digits - 1; i >= 0; i--) {
    buffer[i] = (index & mask) != 0 ? '1' : '0';
    mask <<= 1;
return new String(buffer);
...
boolean[]toBinaryArray(int input, int length)
Returns an integer converted to a binary array.
boolean[] bits = new boolean[length];
for (int i = length - 1; i >= 0; i--) {
    bits[i] = (input & (1 << i)) != 0;
return bits;
boolean[]toBinaryArray(int integer, int size)
Converts an integer to a binary array with a specified max size input: 11, output: [1011]
boolean[] b = new boolean[size];
String s = Integer.toBinaryString(integer);
if (s.length() > b.length)
    s = s.substring(s.length() - b.length);
for (int i = s.length() - 1; i >= 0; i--)
    b[s.length() - 1 - i] = (s.charAt(i) == '1');
return b;
byte[]toBinaryBoolean(boolean source)
to Binary Boolean
return new byte[] { (byte) (source ? 0x01 : 0x00) };
chartoBinaryChar(boolean bit)
to Binary Char
return bit ? '1' : '0';
StringtoBinaryClassName(String fileName)
Converts a class's file name to a binary name.
if (fileName.startsWith("/")) {
    fileName = fileName.substring(1);
if (fileName.endsWith(".class")) {
    fileName = fileName.substring(0, fileName.length() - 6);
fileName = fileName.replace("/", ".");
return fileName;
...
bytetoBinaryFromHex(byte ch)
Takes a ASCII digit in the range A-F0-9 and returns the corresponding integer/ordinal value.
if (ch >= 'A' && ch <= 'F')
    return (byte) ((byte) 10 + (byte) (ch - 'A'));
return (byte) (ch - '0');