0

I'm getting illegal unicode escape for the following code.

 for(int i=3400;i<4000;i++)
   System.out.println("\u" + i );

If I add a slash before I get \u3400 as output instead of actual unicode character.

I want to print the unicode characters in a loop. Also unicode characters are hex codes. How to loop through hex codes and print all unicode characters.

Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
Sorter
  • 9,704
  • 6
  • 64
  • 74
  • Please go through : http://stackoverflow.com/questions/6230190/convert-international-string-to-u-codes-in-java – R.G Nov 12 '15 at 07:29

3 Answers3

5

You cannot concatenate "\u" with something at runtime, because "\uXXXX" sequences are parsed during the compilation. However don't need to do this. You can simply cast integers to chars and use 0x prefix to specify the hex numbers:

for(int i=0x3400;i<0x4000;i++)
    System.out.println((char)i);
Tagir Valeev
  • 97,161
  • 19
  • 222
  • 334
2

What you are looking for is something like:

    for (int i = 0x3400; i < 0x4000; i++) {
        System.out.println((char) i);
    }

You shouldn't forget, that any number after \u prefix is hexademical number (radix 16). From this fact follows that in your loop you will lose a lot of unicode characters in hex-interval 3400...4000. So, you should change loop range to 0x3400 and 0x4000.

Andremoniy
  • 34,031
  • 20
  • 135
  • 241
1

you can use format functionality:-

for(int i=3400;i<4000;i++){
   System.out.format("\\u%04x", i);
}

Or according to this answer use this :-

for(int i=3400;i<4000;i++){
   System.out.println( "\\u" + Integer.toHexString(i| 0x10000).substring(1));
}
Community
  • 1
  • 1
karim mohsen
  • 2,164
  • 1
  • 15
  • 19