Java Integer to Binary intToBinary(int binary, int digits)

Here you can find the source of intToBinary(int binary, int digits)

Description

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

License

Apache License

Parameter

Parameter Description
binary int
digits int

Return

String

Declaration

private static String intToBinary(int binary, int digits) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

public class Main {
    /**//from ww w.  jav a2  s .  com
     * 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
     * @param binary int
     * @param digits int
     * @return String
     */
    private static String intToBinary(int binary, int digits) {
        String temp = Integer.toBinaryString(binary);
        int foundDigits = temp.length();
        String returner = temp;
        for (int i = foundDigits; i < digits; i++) {
            returner = "0" + returner;
        }
        return returner;
    }
}

Related

  1. int2binString(int x)
  2. integerToBin(int value)
  3. integerToBinaryString(int value)
  4. integerToBinaryString(int value, int numberOfBits)
  5. integerToBinString(final int aValue, final int aFieldWidth)
  6. intToBinary(int i)
  7. intToBinary(int i, int byteLength)
  8. intToBinary(long value, int bits)
  9. intToBinString(int val, int width)