简体   繁体   中英

How to convert hexstring to ANSI (window 1252) and convert ANSI (window 1252) back to hex string in Java?

How to convert hex string to ansi (window 1252) and ansi (window 1252)to hex string in Java.

python (Works perfectly)

q = "hex string value"

x = bytes.fromhex(q).decode('ANSI')

a = x.encode("ANSI")
a = a.hex()
if q==a:
    print("Correct")

Java (This code has a problem)

String hexOri = "hex string value";
StringBuilder output = new StringBuilder();
for (int i = 0; i < hexOri.length(); i+=2) {
    String str = hexOri.substring(i, i+2);
    output.append((char)Integer.parseInt(str, 16));
}
System.out.println("ANSI = " + output);
char [] chars = output.toString().toCharArray();
StringBuffer hexOutput = new StringBuffer();

for(int i = 0; i < chars.length; i++){
  hexOutput.append(Integer.toHexString((int)chars[i]));
}
System.out.println("HexOutput = " + hexOutput.toString());
System.out.println(hexOri.equals(hexOutput.toString()));

Output from Python

Correct

Expected Output from Python

Correct

Output from Java

False

Expected Output from Java

Correct

In java the strings are encoded in UTF-16, so you can't read simply read/write the bytes of a string to get the encoding representation you desire.

You should use String#getBytes(String str, String charset) to get the string converted in the encoding you need and serialized to a byte array.

The same thing must be done to decode a byte array, using new String(buffer,encoding) .

In both cases if you use the method without the charset it will use the default encoding for the JVM instance (which should be the system charset).

    public static void main(String[] args) {
        String str = "\tSome text [à]";

        try {
            System.out.println(str); //     Some text [à]

            String windowsLatin1 = "Cp1252";
            String hexString = toHex(windowsLatin1, str);

            System.out.println(hexString); // 09536f6d652074657874205be05d

            String winString = toString(windowsLatin1, hexString);

            System.out.println(winString); //   Some text [à]
        } catch (UnsupportedEncodingException e) {
            // Should not happen.
        }

    }

    public static String toString(String encoding, String hexString) throws UnsupportedEncodingException {
        int length = hexString.length();
        byte [] buffer = new byte[length/2];
        for (int i = 0; i < length ; i+=2) {
            String hexVal = hexString.substring(i,i+2);
            byte code = (byte) Integer.parseInt(hexVal,16);
            buffer[i/2]=code;
        }
        String winString = new String(buffer,encoding);
        return winString;
    }

    public static String toHex(String encoding, String str) throws UnsupportedEncodingException {
        byte[] bytes = str.getBytes(encoding);
        StringBuilder builder = new StringBuilder();
        for (int i = 0; i < bytes.length; i++) {
            byte b = bytes[i];
            String hexChar = Integer.toHexString(b & 0xff);
            if(hexChar.length()<2) {
                builder.append('0');
            }
            builder.append(hexChar);
        }
        String hexString = builder.toString(); // 09536f6d652074657874205be05d
        return hexString;
    }

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