如何将字符串转换为Java中的十六进制 [英] How can I convert a string into hex in Java

查看:158
本文介绍了如何将字符串转换为Java中的十六进制的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将字符串转换为十六进制,并决定使用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));
文件hexfile = new File(hexfile.txt);
FileWriter writer = new FileWriter(hexfile);
writer.write(输出);
writer.close();

完整测试输入字符串

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

完整测试输出十六进制

  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 5 253 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 eeeb eced eeef
f0f1 f2f3 f4f5 f6f7 f8f9 fafb fcfd feff


解决方案

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

示例:

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

输出:

  3F3F3F3F3F3F 

挂在 byte [] code> parseHexBinary 输出并设置一个断点,以便您可以直接检查它的内容,您会注意到它包含正确的二进制值。



每条评论更新

如果您想要写入 parseHexBinary 到一个文件中,你应该将原始字节写入文件,而不是字节的字符串。

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


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?

    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

解决方案

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.

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 parseHexBinary outputs and set a breakpoint so you can examine it's contents directly, you'll notice that it contains the correct binary values.

Update per comments:

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

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

这篇关于如何将字符串转换为Java中的十六进制的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆