Java Binary Encode toBin(int size, String bitString)

Here you can find the source of toBin(int size, String bitString)

Description

to Bin

License

Open Source License

Parameter

Parameter Description
size a parameter
bitString a parameter

Declaration

private static String toBin(int size, String bitString) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

public class Main {
    /**//from www  .ja  v  a2  s  .  c om
     * Returns the binary representation of an integer.
     *
     * @param i - the integer to format as a binary String.
     * @return the binary representation of the given integer.
     */
    public static String toBin(int i) {
        return toBin(32, Integer.toBinaryString(i));
    }

    /**
     * Returns the binary representation of a byte.
     *
     * @param b - the byte to format as a binary String.
     * @return the binary representation of the given byte.
     */
    public static String toBin(byte b) {
        return toBin(8, Integer.toBinaryString(b & 0xff));
    }

    /**
     *
     *
     * @param size
     * @param bitString
     * @return
     */
    private static String toBin(int size, String bitString) {
        return String.format(String.format("%%%ds", size), bitString).replace(' ', '0');
    }
}

Related

  1. toBin(byte n)
  2. toBin(int bin, int size)
  3. toBin(int x)
  4. toBin(long value, int width)
  5. toBinArray(String hexStr)
  6. toBinary(byte b)