Java Binary to Hex bin2Hex(String bin)

Here you can find the source of bin2Hex(String bin)

Description

bin Hex

License

Open Source License

Declaration

public static String bin2Hex(String bin) 

Method Source Code

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

public class Main {
    public static String bin2Hex(String bin) {
        int intVal = parseBin(bin);
        return normalizeNumber(Integer.toHexString(intVal), bin.length() / 4);
    }/*  w ww  .ja  v a2s .com*/

    public static int parseBin(String bin) {
        try {
            return (int) Long.parseLong("0" + bin, 2);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return 0;

    }

    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. bin2Hex(byte[] b)
  2. bin2Hex(byte[] bin)
  3. bin2hex(final byte[] b)
  4. bin2hex(final byte[] base)
  5. bin2hex(int digit)
  6. bin2hex(String bin)
  7. bin2hex(String str)
  8. binaryString2hexString(String bString)
  9. binaryToHex(byte[] ba)