如何在Java中将字符串转换为十六进制

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18946597/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 12:48:42  来源:igfitidea点击:

How can I convert a string into hex in Java

javastringhex

提问by user2804671

I'm trying to convert string into hex and decided to use DatatypeConverter.parseHexBinary, it worked in most of cases, but there are some exceptions, like 8f, it's converted into x'3f', instead of x'8f', so I wrote simple test, it turned out to be, the same thing is happening for 81, 8d, 8f, 90, 9d, they are all wrongly converted into x'3f', did I do anything wrong? can I use parseHexBinary to do the conversion, if not, what should I use?

我正在尝试将字符串转换为十六进制并决定使用 DatatypeConverter.parseHexBinary,它在大多数情况下都有效,但也有一些例外,例如 8f,它被转换为 x'3f',而不是 x'8f',所以我写了一个简单的测试,结果是,同样的事情发生在 81, 8d, 8f, 90, 9d 上,它们都被错误地转换成 x'3f',我做错了什么吗?我可以使用 parseHexBinary 进行转换,如果不能,我应该使用什么?

    String input = "818D8F909D";
    String output = new String(DatatypeConverter.parseHexBinary(input));
    File hexfile = new File("hexfile.txt");
    FileWriter writer = new FileWriter(hexfile);
    writer.write(output);
    writer.close();

full test input string

完整的测试输入字符串

000102030405060708090A0B0C0D0E0F
101112131415161718191A1B1C1D1E1F
202122232425262728292A2B2C2D2E2F
303132333435363738393A3B3C3D3E3F
404142434445464748494A4B4C4D4E4F
505152535455565758595A5B5C5D5E5F
606162636465666768696A6B6C6D6E6F
707172737475767778797A7B7C7D7E7F
808182838485868788898A8B8C8D8E8F
909192939495969798999A9B9C9D9E9F
A0A1A2A3A4A5A6A7A8A9AAABACADAEAF
B0B1B2B3B4B5B6B7B8B9BABBBCBDBEBF
C0C1C2C3C4C5C6C7C8C9CACBCCCDCECF
D0D1D2D3D4D5D6D7D8D9DADBDCDDDEDF
E0E1E2E3E4E5E6E7E8E9EAEBECEDEEEF
F0F1F2F3F4F5F6F7F8F9FAFBFCFDFEFF

full test output hex

完整的测试输出十六进制

0001 0203 0405 0607 0809 0a0b 0c0d 0e0f
1011 1213 1415 1617 1819 1a1b 1c1d 1e1f
2021 2223 2425 2627 2829 2a2b 2c2d 2e2f
3031 3233 3435 3637 3839 3a3b 3c3d 3e3f
4041 4243 4445 4647 4849 4a4b 4c4d 4e4f
5051 5253 5455 5657 5859 5a5b 5c5d 5e5f
6061 6263 6465 6667 6869 6a6b 6c6d 6e6f
7071 7273 7475 7677 7879 7a7b 7c7d 7e7f
803f 8283 8485 8687 8889 8a8b 8c3f 8e3f
3f91 9293 9495 9697 9899 9a9b 9c3f 9e9f
a0a1 a2a3 a4a5 a6a7 a8a9 aaab acad aeaf
b0b1 b2b3 b4b5 b6b7 b8b9 babb bcbd bebf
c0c1 c2c3 c4c5 c6c7 c8c9 cacb cccd cecf
d0d1 d2d3 d4d5 d6d7 d8d9 dadb dcdd dedf
e0e1 e2e3 e4e5 e6e7 e8e9 eaeb eced eeef
f0f1 f2f3 f4f5 f6f7 f8f9 fafb fcfd feff

回答by Rahul Tripathi

You can try like this(Kaleb Pederson answers this):-

你可以尝试像这样(卡莱布佩德森回答了这个): -

public String toHex(String arg) {
    return String.format("%040x", new BigInteger(1, arg.getBytes(/*YOUR_CHARSET?*/)));
}

or try this:-

或试试这个:-

String s= "000102030405060708090A0B0C0D0E0F";    
byte[] b= Hex.decodeHex(s.toCharArray());
System.out.println(new String(b, "UTF8"));

回答by lreeder

Something must be wrong with the code you use to convert the byte array to strings. Try DataTypeConverter.printHexBinary() instead of your custom code to convert a set of bytes back to the hex string. This works for me:

您用于将字节数组转换为字符串的代码一定有问题。尝试使用 DataTypeConverter.printHexBinary() 而不是您的自定义代码将一组字节转换回十六进制字符串。这对我有用:

String hex = "808182838485868788898A8B8C8D8E8F";
byte[] hexAsBytes = DatatypeConverter.parseHexBinary(hex);
String output =  DatatypeConverter.printHexBinary(hexAsBytes);
System.out.println("Conversion returns " + output);

Prints:

印刷:

Conversion returns 808182838485868788898A8B8C8D8E8F

Conversion returns 808182838485868788898A8B8C8D8E8F

回答by Syon

The issue here is that you're trying to create a String from binary data, and the default encoding used by the String class is changing the value of that binary.

这里的问题是您试图从二进制数据创建一个 String,而 String 类使用的默认编码正在更改该二进制文件的值。

Example:

例子:

String output = new String(new byte[]{(byte)0x90, (byte)0x81, (byte)0x8d, (byte)0x8f, (byte)0x90, (byte)0x9d});
System.out.println(DatatypeConverter.printHexBinary(output.getBytes()));

Output:

输出:

3F3F3F3F3F3F

Hang on to the byte[]that parseHexBinaryoutputs and set a breakpoint so you can examine it's contents directly, you'll notice that it contains the correct binary values.

悬挂到byte[]parseHexBinary输出,并设置一个断点,所以你可以检查它的内容直接,你会发现它包含正确的二进制值。

Update per comments:

根据评论更新:

If you want to write the output of parseHexBinaryto a file, you should write the raw bytes to the file, not a String of the bytes.

如果要将输出写入parseHexBinary文件,则应将原始字节写入文件,而不是字节字符串。

DataOutputStream os = new DataOutputStream(new FileOutputStream("someFileName"));
byte[] bytes = DatatypeConverter.parseHexBinary(input);
os.write(bytes, 0, bytes.length);
os.close();

回答by lreeder

If you want to write the raw bytes of your hex string to a file (not the hex string representing the raw bytes themselves), don't use FileWriter, and don't convert the raw byte array to a String. According to the Javadoc for FileWriter:

如果要将十六进制字符串的原始字节写入文件(不是表示原始字节本身的十六进制字符串),请不要使用 FileWriter,也不要将原始字节数组转换为字符串。 根据 FileWriter 的 Javadoc

FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.

FileWriter is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.

Here's your code using FileOutputStream:

这是您使用 FileOutputStream 的代码:

String input = "808182838485868788898A8B8C8D8E8F";
byte[] output = DatatypeConverter.parseHexBinary(input);
File hexfile = new File("hexfile.txt");
FileOutputStream fos = new FileOutputStream(hexfile);
writer.write(output);
writer.close();

And here is a screenshot of a hex editor showing the contents of the file:

这是显示文件内容的十六进制编辑器的屏幕截图:

screenshot of hex editor

十六进制编辑器的屏幕截图

回答by Fakher

if you are sure that all your numbers are between [0-9] and [A-F] (mean that they are all hexadecimal numbers) than you can use this method to convert your number:

如果您确定所有数字都在 [0-9] 和 [AF] 之间(意味着它们都是十六进制数字),那么您可以使用此方法来转换您的数字:

private int hexaStringToInteger(String input){
        int res = 0;
        int length = input.length()-2;
        for(int i=0;i<length+1;i++){
            char currNumber = input.charAt(i);
            switch (currNumber){
                case 'A':
                    res += 10 * Math.pow(16,length-i);
                    break;
                case 'B':
                    res += 11 * Math.pow(16,length-i);
                    break;
                case 'C':
                    res += 12 * Math.pow(16,length-i);
                    break;
                case 'D':
                    res += 13 * Math.pow(16,length-i);
                    break;
                case 'E':
                    res += 14 * Math.pow(16,length-i);
                    break;
                case 'F':
                    res += 15 * Math.pow(16,length-i);
                    break;
                default:
                    int number = Integer.parseInt(currNumber + "");
                    res += number * Math.pow(16,length-i);
                    break;
            }
        }
        return res;
    }