0

Hi I have a small function which prints byte to Hindi which is stored as Unicode. My function is like

public static void byteArrayToPrintableHindi(byte[] iData) {

    String value = "";
    String unicode = "\\u";
    StringBuilder sb = new StringBuilder();
    for (int i = 0; i < iData.length; i++) {
        if (i % 2 == 0) {

            value = value.concat(unicode.concat(String.format("%02X", iData[i])));
            sb.append(String.format("%02X", iData[i]));
        } else {
            value = value.concat(String.format("%02X", iData[i]));
        }
    }
    System.out.println("value = "+value);
    System.out.println("\u091A\u0941\u0921\u093C\u093E\u092E\u0923\u093F");

} 

and the output is

value = \u091A\u0941\u0921\u093C\u093E\u092E\u0923\u093F
चुड़ामणि

I am expecting the value to print

चुड़ामणि

I don't know why it is not printing the desired output.

Robert
  • 10,403
  • 14
  • 67
  • 117
Avinash Sahu
  • 249
  • 3
  • 19

1 Answers1

4

You're misunderstanding how \uXXXX escape codes work. When the Java compiler reads your source code, it interprets those escape codes and translates them to Unicode characters. You cannot at runtime build a string that consists of \uXXXX codes and expect Java to automatically translate that into Unicode characters - that's not how it works. It only works with literal \uXXXX codes in your source code.

You can simply do this:

public static void byteArrayToPrintableHindi(byte[] iData) throws UnsupportedEncodingException {
    String value = new String(iData, "UTF-16");

    System.out.println("value = "+value);
    System.out.println("\u091A\u0941\u0921\u093C\u093E\u092E\u0923\u093F");
}

assuming that the data is UTF-16-encoded.

Jesper
  • 202,709
  • 46
  • 318
  • 350