简体   繁体   中英

How can I convert a string into hex in Java

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

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"));

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

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:

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

Prints:

Conversion returns 808182838485868788898A8B8C8D8E8F

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 is meant for writing streams of characters. For writing streams of raw bytes, consider using a FileOutputStream.

Here's your code using 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:

hex编辑器的截图

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM