Java Utililty Methods Integer to Binary

List of utility methods to do Integer to Binary

Description

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

Method

Stringint2binString(int x)
intbin String
String s = "";
for (int ii = 0; ii < 32; ii++) {
    if ((ii > 0) && (ii % 4 == 0)) {
        s = " " + s;
    s = hex_digit[(x & (1 << ii)) >>> ii] + s;
return s;
...
StringintegerToBin(int value)
integer To Bin
return Long.toBinaryString(0x0000000100000000L | ((value) & 0x00000000FFFFFFFFL)).substring(1);
StringintegerToBinaryString(int value)
integer To Binary String
return (value < 0 ? "1" : "0") + Integer.toBinaryString(Math.abs(value));
StringintegerToBinaryString(int value, int numberOfBits)
Converts a value from a normal integer to a binary value of type String.
StringBuilder stringBuilder = new StringBuilder();
stringBuilder.append(Integer.toBinaryString(value));
while (stringBuilder.length() != numberOfBits) {
    String old = stringBuilder.toString();
    stringBuilder = new StringBuilder();
    stringBuilder.append("0" + old);
String finalizedBinaryValue = stringBuilder.toString();
...
StringintegerToBinString(final int aValue, final int aFieldWidth)
converts an integer to a bin string with leading zeros
final long mask = (long) (Math.pow(2.0, aFieldWidth) - 1L);
StringBuilder sb = new StringBuilder(Long.toBinaryString(aValue & mask));
int numberOfLeadingZeros = Math.max(0, aFieldWidth - sb.length());
for (; numberOfLeadingZeros > 0; numberOfLeadingZeros--) {
    sb.insert(0, '0');
return sb.toString();
StringintToBinary(int binary, int digits)
Converts the given integer to a String representing a binary number with the specified number of digits For example when using 4 digits the binary 1 is 0001
String temp = Integer.toBinaryString(binary);
int foundDigits = temp.length();
String returner = temp;
for (int i = foundDigits; i < digits; i++) {
    returner = "0" + returner;
return returner;
char[]intToBinary(int i)
int To Binary
char[] values = new char[32];
for (int pos = 31; pos >= 0; pos--, i >>= 1) {
    if ((i & (int) 0x01) == (int) 1)
        values[pos] = '1';
    else
        values[pos] = '0';
return values;
...
StringintToBinary(int i, int byteLength)
int To Binary
String binString = Integer.toBinaryString(i);
while (binString.length() < byteLength * 8) {
    binString = "0" + binString;
return binString;
StringintToBinary(long value, int bits)
This method converts int values to binary-string.
char[] returnValue = new char[bits];
boolean wasNegative = false;
if (value < 0) {
    wasNegative = true;
    ++value;
    value = (value * -1);
for (int i = 0; i < bits; ++i) {
...
StringintToBinString(int val, int width)
int To Bin String
String str = Integer.toBinaryString(val);
if (str.length() >= width) {
    return str;
return mZeroes.substring(0, width - str.length()) + str;