Java Binary Encode toBin(int bin, int size)

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

Description

to Bin

License

Open Source License

Declaration

public static String toBin(int bin, int size) 

Method Source Code

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

public class Main {
    public static String toBin(int bin, int size) {
        String ret = Integer.toBinaryString(bin);
        return normalizeNumber(ret, size);
    }/*from w w w  .  j  a  v  a  2 s  .c  o  m*/

    private static String normalizeNumber(String ret, int size) {
        int addCount = size - ret.length();
        if (addCount > 0) {
            for (int i = 0; i < addCount; i++) {
                ret = "0" + ret;
            }
        } else {
            ret = ret.substring(ret.length() - size, ret.length());
        }
        return ret;
    }
}

Related

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