简体   繁体   English

使用十六进制代码循环打印Unicode字符

[英]Print Unicode characters with hex code loop

I'm getting illegal unicode escape for the following code. 我正在为以下代码非法转义unicode。

 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. 如果在输出\㐀而不是实际的unicode字符之前添加斜杠。

I want to print the unicode characters in a loop. 我想循环打印unicode字符。 Also unicode characters are hex codes. unicode字符也是十六进制代码。 How to loop through hex codes and print all unicode characters. 如何遍历十六进制代码并打印所有unicode字符。

You cannot concatenate "\\u\u0026quot; with something at runtime, because "\\uXXXX" sequences are parsed during the compilation. 您不能在运行时将"\\u\u0026quot;与某些内容连接在一起,因为在编译过程中会解析"\\uXXXX"序列。 However don't need to do this. 但是不需要这样做。 You can simply cast integers to chars and use 0x prefix to specify the hex numbers: 您可以简单地将整数转换为char并使用0x前缀指定十六进制数字:

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). 您不要忘记, \\u\u003c/code>前缀后面的任何数字都是十六进制数字(基数16)。 From this fact follows that in your loop you will lose a lot of unicode characters in hex-interval 3400...4000 . 从这个事实可以得出,在循环中,您将在十六进制间隔3400...4000丢失很多unicode字符。 So, you should change loop range to 0x3400 and 0x4000 . 因此,您应该将循环范围更改为0x34000x4000

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM