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

StringtoBinaryString(int number)
Returns a string representation of the integer argument as an unsigned integer in base 2.
String bin = "";
for (int i = 1; i < Integer.SIZE; ++i) {
    if (i % 8 == 0) {
        bin = " " + (number & FIRST_BIT_MASK) + bin;
    } else {
        bin = (number & FIRST_BIT_MASK) + bin;
    number = number >> 1;
...
StringtoBinaryString(int number, int length)
to Binary String
String result = new String(Integer.toBinaryString(number));
while (result.length() < length) {
    result = '0' + result;
return result;
StringtoBinaryString(int value)
Helper.
return toBinaryString(value, 32);
StringtoBinaryString(int[] input)
to Binary String
String result = "";
int i;
for (i = 0; i < input.length; i++) {
    int e = input[i];
    for (int ii = 0; ii < 32; ii++) {
        int b = (e >>> ii) & 1;
        result += b;
    if (i != input.length - 1) {
        result += " ";
return result;
CharSequencetoBinaryString(long val)
to Binary String
StringBuilder str = new StringBuilder(64);
int bits = 64;
while (bits-- != 0) {
    str.append(((val >>> bits) & 1) != 0 ? '1' : '0');
return str;
StringtoBinaryString(long value, int bitLegnth)
Helper.
return Long.toBinaryString(value).substring(0, bitLegnth);
StringtoBinaryStringAddress(long address)
to Binary String Address
return "0x" + Long.toBinaryString(address).toUpperCase();
StringtoBinaryText(StringBuffer buf)
to Binary Text
boolean bufHasBinary = false;
int len = buf.length();
for (int i = 0; i < len; i++) {
    if (buf.charAt(i) < ' ') {
        bufHasBinary = true;
        break;
if (bufHasBinary) {
    StringBuffer formatedDataBuf = new StringBuffer();
    for (int k = 0; k < len; k++) {
        formatedDataBuf.append(printable(buf.charAt(k)));
    formatedDataBuf.append(" 0x[");
    for (int k = 0; k < len; k++) {
        formatedDataBuf.append(toHex00String(buf.charAt(k))).append(' ');
    formatedDataBuf.append("]");
    buf = formatedDataBuf;
return buf.toString();
StringtoBinFromByte(final byte b)
Transform a byte into a bitstring (of length 8)
StringBuilder binBuffer = new StringBuilder(8);
binBuffer.append(binSymbols[((b & 0x80) >>> 7)]);
binBuffer.append(binSymbols[((b & 0x40) >>> 6)]);
binBuffer.append(binSymbols[((b & 0x20) >>> 5)]);
binBuffer.append(binSymbols[((b & 0x10) >>> 4)]);
binBuffer.append(binSymbols[((b & 0x08) >>> 3)]);
binBuffer.append(binSymbols[((b & 0x04) >>> 2)]);
binBuffer.append(binSymbols[((b & 0x02) >>> 1)]);
...
StringtoBinFromHex(final String hexSymbols)
Transform a string of hex symbols to a string of binary symbols
String hex = stripHexPrefix(hexSymbols);
StringBuilder bits = new StringBuilder(hex.length() * BITS_PER_HEX_DIGIT);
for (int i = 0; i < hex.length(); i++) {
    bits.append(toBinFromHexChar(hex.charAt(i)));
return (bits.toString());