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

时间:2011-12-27 03:13:04

标签: java string

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

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

然而,这仅适用于Hex 0 - 9;它不适用于Hex A - F,因为它使用int。任何人都可以增强它吗?

6 个答案:

答案 0 :(得分:27)

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

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

答案 1 :(得分:14)

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

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

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

答案 2 :(得分:6)

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

Integer.parseInt(hex, 16);

答案 3 :(得分:5)

以下是我为操作hex,纯文本和二进制文件而编写的一些例程,希望它们有所帮助。由于我从这些主题中借鉴了想法,我想我会分享。

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();
}

}

答案 4 :(得分:4)

接受的答案仅适用于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;
}

答案 5 :(得分:1)

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;
}

对不起,这有点晚了。但是,我认为我的答案是最直接和最简单的答案。

此致 云汉轩 2018年9月2日