Convert hexadecimal string (hex) to a binary string

后端 未结 7 2031
孤独总比滥情好
孤独总比滥情好 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: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;
        }
    

提交回复
热议问题