java - String Hex to String二进制转换器

时间:2016-04-05 12:48:37

标签: java string binary hex

//convert hexadecimal to binary using BigInt
public static String hexToBinary(String hexNumber)
{
    //BigInteger temp = new BigInteger(hexNumber, 16);
    //return temp.toString(2);
        String s = hexNumber;
        byte[] bytes = s.getBytes();
        StringBuilder binary = new StringBuilder();
        for (byte b : bytes)
        {
            int val = b;
            for (int i = 0; i < 8; i++)
            {
                binary.append((val & 128) == 0 ? 0 : 1);
                val <<= 1;
            }
            binary.append(' ');
        }
        System.out.println("'" + s + "' to binary: " + binary);
        return binary.toString();
}

字符串hexNumber为“0957”,函数返回“00110000 00111001 00110101 00110111”......我原以为“0000 1001 0101 0111”。有人可以确定问题是什么吗?感谢。

3 个答案:

答案 0 :(得分:4)

您正在转换字符的数字值&#39; 0&#39;,&#39; 9&#39;&#39; 5&#39;&#39; 7&#39;二进制,而不是转换实际数字。例如,&#39; 0&#39;的数值是48,这就是你打印的第一个字节为00110000的原因。

在将每个字符转换为位之前,您需要从每个字符中减去48(或&#39; 0&#39;),并且还要记住每个十六进制字符可以用4位表示,但是您的循环为每个字符打印8位字符。实际上只有当你的输入没有十六进制数字A到F时才会起作用(从十六进制数字A到F你必须减去('A' - 10)才能将它们转换为数字10到15)。

以下是一种方法(仍然缺少数字处理&#39; A&#39; F&#39; F&#39;但适用于您的样本输入):

    byte[] bytes = s.getBytes();
    StringBuilder binary = new StringBuilder();
    for (byte b : bytes)
    {
        int val = b - '0';
        val <<= 4;
        for (int i = 0; i < 4; i++)
        {
            binary.append((val & 128) == 0 ? 0 : 1);
            val <<= 1;
        }
        binary.append(' ');
    }
    System.out.println("'" + s + "' to binary: " + binary);

输出:

'0957' to binary: 0000 1001 0101 0111 

答案 1 :(得分:1)

以下是两种方法:

public class Snippet {
    public static String hexToBinary1(String hexNumber)
    {
        int v = Integer.parseInt(hexNumber, 16);
        String s = Integer.toString(v, 2); 
        int pad = hexNumber.length()*4-s.length();
        for (int i=0; i<pad; i++) {
            s="0"+s;
        }
        return s;
    }

    public static String hexToBinary2(String hexNum) {
        StringBuilder sb = new StringBuilder();
        for (int i=0; i<hexNum.length(); i++) {
            int v = Character.digit(hexNum.charAt(i), 16);
            String padded = String.format("%4s",  Integer.toString(v, 2)).replace(' ', '0');
            sb.append(padded);
        }
        return sb.toString();
    }

    public static void main(String[] arjgs) {
        System.out.println(hexToBinary1("0957"));
        System.out.println(hexToBinary2("0957"));
    }
}

两者都将打印出0000100101010111。

答案 2 :(得分:0)

String char  binary number
"0" → 0x0030 → 0000 0000 0011 0000
"1" → 0x0031 → 0000 0000 0011 0001
"2" → 0x0032 → 0000 0000 0011 0010
 :
"9" → 0x0039 → 0000 0000 0011 1001
"A" → 0x0041 → 0000 0000 0100 0001
"B" → 0x0042 → 0000 0000 0100 0010
 :
"F" → 0x0046 → 0000 0000 0100 0110

http://www.tamasoft.co.jp/en/general-info/unicode.html

public static String hexToBinary1(String hexNumber) {

    StringBuilder binary = new StringBuilder();

    for (int i = 0; i < str.length(); i++) {

        char ch = str.charAt(i);
        int val;

        if(ch >= '0' && ch <= '9') {
            val = (int)ch - 0x30;
        else if(ch >= 'A' && ch <= 'F') {
            val = (int)ch - 0x41 + 0x0A;
        }

        for (int i = 0; i < 4; i++) {
            binary.append((val & 16) == 0 ? 0 : 1);
            val <<= 1;
        }
        binary.append(' ');
    }

    System.out.println("'" + s + "' to binary: " + binary);
    return binary.toString();
}

public static String hexToBinary1(String hexNumber) {
    return String.format("%16s", Integer.toBinaryString(Integer.parseInt(hexNumber, 16))).replace(" ", "0");
}