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

时间:2021-06-06 15:45:12

I would like to convert a hex string to a binary string. For example, Hex 2 is 0010. Below is the code:

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

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

However this only works for Hex 0 - 9; it won't work for Hex A - F because it uses int. Can anyone enhance it?

但是这只适用于十六进制0 - 9;它不会为Hex A - F工作,因为它使用int。谁能提高它?

6 个解决方案

#1


26  

You need to tell Java that the int is in hex, like this:

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

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

#2


9  

the accepted version will only work for 32 bit numbers.

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

Here's a version that works for arbitrarily long hex strings:

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

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

#3


6  

You need to use the other Integer.parseInt() method.

您需要使用另一个int . parseint()方法。

Integer.parseInt(hex, 16);

#4


5  

Here are some routines I wrote for manipulating hex, plaintext, and binary, hope they help. Since I borrowed ideas from these threads, thought I would share.

下面是我为处理十六进制、明文和二进制编写的一些例程,希望它们能有所帮助。因为我从这些线索中借鉴了一些想法,所以我想和大家分享一下。

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


3  

The accepted answer only works for 32-bit values, and the alternate BigInteger version truncates leading zeros in the binary string! Here's a function that should work in all cases.

接受的答案只适用于32位的值,而备用的BigInteger版本将在二进制字符串中截断前导0 !这是一个在所有情况下都应该适用的函数。

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


0  

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

Sorry, it's been a little bit late. But still, I think my answer is the most straight forward and simple one.

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

Regards, YUN HANXUAN 09.02.2018

问候,YUN HANXUAN 09.02.2018

#1


26  

You need to tell Java that the int is in hex, like this:

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

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

#2


9  

the accepted version will only work for 32 bit numbers.

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

Here's a version that works for arbitrarily long hex strings:

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

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

#3


6  

You need to use the other Integer.parseInt() method.

您需要使用另一个int . parseint()方法。

Integer.parseInt(hex, 16);

#4


5  

Here are some routines I wrote for manipulating hex, plaintext, and binary, hope they help. Since I borrowed ideas from these threads, thought I would share.

下面是我为处理十六进制、明文和二进制编写的一些例程,希望它们能有所帮助。因为我从这些线索中借鉴了一些想法,所以我想和大家分享一下。

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


3  

The accepted answer only works for 32-bit values, and the alternate BigInteger version truncates leading zeros in the binary string! Here's a function that should work in all cases.

接受的答案只适用于32位的值,而备用的BigInteger版本将在二进制字符串中截断前导0 !这是一个在所有情况下都应该适用的函数。

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


0  

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

Sorry, it's been a little bit late. But still, I think my answer is the most straight forward and simple one.

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

Regards, YUN HANXUAN 09.02.2018

问候,YUN HANXUAN 09.02.2018