简体   繁体   中英

Print Unicode characters with hex code loop

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 \㐀 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.

You cannot concatenate "\\u\u0026quot; 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);

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

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\u003c/code> 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 .

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