Skip to content
Advertisement

Hex to Cyrillic text

I have hex byte string like “D09FD0B5D180D0BDD0B8D0BA” this is “Перник”.

For each letter of the Cyrillic alphabet, I need 2 bytes.

For “П” I need “D0 9F”.

If I use:

char letter = (char) 1055; // this is "П"

My problem is how from hex “D0 9F” to get int value “1055”. Or how from “D09FD0B5D180D0BDD0B8D0BA” to convert to “Перник”.

Answer

You didn’t specified the encoding, however it appears to be UTF-8 so the character П is not encoded as 041F (dec. 1055), but as D09F (dec. 53407).

Note also that UTF-8 is a variable length encoding, so the assumption 2 byte / char may be valid for the Cyrillic alphabet but not in general.

import java.nio.charset.StandardCharsets;

public class Hex2String {
    public static String hex2String(String hex) {
        byte[] b=new byte[hex.length()/2];
        for (int i=0;i<b.length;i++) {
            b[i]=(byte) Integer.parseInt(hex, i*2, i*2+2, 16);
        }
        return new String(b, StandardCharsets.UTF_8);
    }
    
    public static void main(String[] args) {
        System.out.println(hex2String("D09FD0B5D180D0BDD0B8D0BA"));
    }
}
Advertisement