Convert hexadecimal string (hex) to a binary string

后端 未结 7 1986
孤独总比滥情好
孤独总比滥情好 2020-11-30 07:31

I found the following way hex to binary conversion:

String binAddr = Integer.toBinaryString(Integer.parseInt(hexAddr, 16)); 

While this app

相关标签:
7条回答
  • 2020-11-30 07:56

    With all zeroes:

    static String hexToBin(String s) {
        String preBin = new BigInteger(s, 16).toString(2);
        Integer length = preBin.length();
        if (length < 8) {
            for (int i = 0; i < 8 - length; i++) {
                preBin = "0" + preBin;
            }
        }
        return preBin;
    }
    
    0 讨论(0)
  • 2020-11-30 07:57

    Fast, and works for large strings:

        private String hexToBin(String hex){
            hex = hex.replaceAll("0", "0000");
            hex = hex.replaceAll("1", "0001");
            hex = hex.replaceAll("2", "0010");
            hex = hex.replaceAll("3", "0011");
            hex = hex.replaceAll("4", "0100");
            hex = hex.replaceAll("5", "0101");
            hex = hex.replaceAll("6", "0110");
            hex = hex.replaceAll("7", "0111");
            hex = hex.replaceAll("8", "1000");
            hex = hex.replaceAll("9", "1001");
            hex = hex.replaceAll("A", "1010");
            hex = hex.replaceAll("B", "1011");
            hex = hex.replaceAll("C", "1100");
            hex = hex.replaceAll("D", "1101");
            hex = hex.replaceAll("E", "1110");
            hex = hex.replaceAll("F", "1111");
            return hex;
        }
    
    0 讨论(0)
  • 2020-11-30 08:09
    public static byte[] hexToBytes(String string) {
     int length = string.length();
     byte[] data = new byte[length / 2];
     for (int i = 0; i < length; i += 2) {
      data[i / 2] = (byte)((Character.digit(string.charAt(i), 16) << 4) + Character.digit(string.charAt(i + 1), 16));
     }
     return data;
    }
    
    0 讨论(0)
  • 2020-11-30 08:19

    BigInteger.toString(radix) will do what you want. Just pass in a radix of 2.

    static String hexToBin(String s) {
      return new BigInteger(s, 16).toString(2);
    }
    
    0 讨论(0)
  • 2020-11-30 08:19
    Integer.parseInt(hex,16);    
    System.out.print(Integer.toBinaryString(hex));
    

    Parse hex(String) to integer with base 16 then convert it to Binary String using toBinaryString(int) method

    example

    int num = (Integer.parseInt("A2B", 16));
    System.out.print(Integer.toBinaryString(num));
    

    Will Print

    101000101011
    

    Max Hex vakue Handled by int is FFFFFFF

    i.e. if FFFFFFF0 is passed ti will give error

    0 讨论(0)
  • 2020-11-30 08:19
    public static byte[] hexToBin(String str)
        {
            int len = str.length();
            byte[] out = new byte[len / 2];
            int endIndx;
    
            for (int i = 0; i < len; i = i + 2)
            {
                endIndx = i + 2;
                if (endIndx > len)
                    endIndx = len - 1;
                out[i / 2] = (byte) Integer.parseInt(str.substring(i, endIndx), 16);
            }
            return out;
        }
    
    0 讨论(0)
提交回复
热议问题