将十六进制字符串转换为二进制字符串

问题描述 投票:0回答:7

我想将十六进制字符串转换为二进制字符串。例如,十六进制 2 是 0010。以下是代码:

String HexToBinary(String Hex)
{
    int i = Integer.parseInt(Hex);
    String Bin = Integer.toBinaryString(i);
    return Bin;
}

但这仅适用于十六进制 0 - 9;它不适用于十六进制 A - F,因为它使用

int
。谁能增强一下吗?

java string
7个回答
39
投票

你需要告诉 Java int 是十六进制的,如下所示:

String hexToBinary(String hex) {
    int i = Integer.parseInt(hex, 16);
    String bin = Integer.toBinaryString(i);
    return bin;
}

22
投票

接受的版本仅适用于 32 位数字。

这是适用于任意长十六进制字符串的版本:

public static String hexToBinary(String hex) {
    return new BigInteger(hex, 16).toString(2);
}

7
投票

接受的答案仅适用于 32 位值,而替代 BigInteger 版本会截断二进制字符串中的前导零!这是一个在所有情况下都适用的函数。

public static String hexToBinary(String hex) {
    int len = hex.length() * 4;
    String bin = new BigInteger(hex, 16).toString(2);

    //left pad the string result with 0s if converting to BigInteger removes them.
    if(bin.length() < len){
        int diff = len - bin.length();
        String pad = "";
        for(int i = 0; i < diff; ++i){
            pad = pad.concat("0");
        }
        bin = pad.concat(bin);
    }
    return bin;
}

6
投票

以下是我编写的一些用于操作十六进制、明文和二进制的例程,希望对您有所帮助。 由于我从这些帖子中借鉴了想法,所以我想分享一下。

public static String zero_pad_bin_char(String bin_char){
    int len = bin_char.length();
    if(len == 8) return bin_char;
    String zero_pad = "0";
    for(int i=1;i<8-len;i++) zero_pad = zero_pad + "0"; 
    return zero_pad + bin_char;
}
public static String plaintext_to_binary(String pt){
    return hex_to_binary(plaintext_to_hex(pt));
}
public static String binary_to_plaintext(String bin){
    return hex_to_plaintext(binary_to_hex(bin));
}
public static String plaintext_to_hex(String pt) {
    String hex = "";
    for(int i=0;i<pt.length();i++){
        String hex_char = Integer.toHexString(pt.charAt(i));
        if(i==0) hex = hex_char;
        else hex = hex + hex_char;
    }
    return hex;  
}
public static String binary_to_hex(String binary) {
    String hex = "";
    String hex_char;
    int len = binary.length()/8;
    for(int i=0;i<len;i++){
        String bin_char = binary.substring(8*i,8*i+8);
        int conv_int = Integer.parseInt(bin_char,2);
        hex_char = Integer.toHexString(conv_int);
        if(i==0) hex = hex_char;
        else hex = hex+hex_char;
    }
    return hex;
}
public static String hex_to_binary(String hex) {
    String hex_char,bin_char,binary;
    binary = "";
    int len = hex.length()/2;
    for(int i=0;i<len;i++){
        hex_char = hex.substring(2*i,2*i+2);
        int conv_int = Integer.parseInt(hex_char,16);
        bin_char = Integer.toBinaryString(conv_int);
        bin_char = zero_pad_bin_char(bin_char);
        if(i==0) binary = bin_char; 
        else binary = binary+bin_char;
        //out.printf("%s %s\n", hex_char,bin_char);
    }
    return binary;
}
public static String hex_to_plaintext(String hex) {
    String hex_char;
    StringBuilder plaintext = new StringBuilder();
    char pt_char;
    int len = hex.length()/2;
    for(int i=0;i<len;i++){
        hex_char = hex.substring(2*i,2*i+2);
        pt_char = (char)Integer.parseInt(hex_char,16);
        plaintext.append(pt_char);
        //out.printf("%s %s\n", hex_char,bin_char);
    }
    return plaintext.toString();
}

}


5
投票

您需要使用其他 Integer.parseInt() 方法。

Integer.parseInt(hex, 16);

4
投票
private static Map<String, String> digiMap = new HashMap<>();

static {
    digiMap.put("0", "0000");
    digiMap.put("1", "0001");
    digiMap.put("2", "0010");
    digiMap.put("3", "0011");
    digiMap.put("4", "0100");
    digiMap.put("5", "0101");
    digiMap.put("6", "0110");
    digiMap.put("7", "0111");
    digiMap.put("8", "1000");
    digiMap.put("9", "1001");
    digiMap.put("A", "1010");
    digiMap.put("B", "1011");
    digiMap.put("C", "1100");
    digiMap.put("D", "1101");
    digiMap.put("E", "1110");
    digiMap.put("F", "1111");
}

static String hexToBin(String s) {
    char[] hex = s.toCharArray();
    String binaryString = "";
    for (char h : hex) {
        binaryString = binaryString + digiMap.get(String.valueOf(h));
    }
    return binaryString;
}

抱歉,有点晚了。但我仍然认为我的答案是最直接、最简单的。


0
投票
public static String hexToBits(String s) {
    String ret = new BigInteger(s, 16).toString(2);
    while (ret.length() % 4 != 0) {
        ret = "0" + ret;
    }
    return ret;
}
© www.soinside.com 2019 - 2024. All rights reserved.