简体   繁体   English

用Java打印前1000个Unicode字符

[英]Print the first 1000 Unicode characters in Java

I need to print the first 1000 unicode characters in Java, the problem is that I don't know how to get together the text and the hexacode for the Unicode. 我需要在Java中打印前1000个unicode字符,问题是我不知道如何将Unicode的文本和十六进制代码放在一起。

Example: I want to print the blank space so in the println command I insert "\\u00\u0026quot; and in the while loop I need to add the other 2 00, but I can't insert them like a text! 示例:我想打印空白,因此在println命令中插入“ \\ u00”,在while循环中我需要添加其他2 00,但是我不能像文本一样插入它们! ("00"). (“ 00”)。

I give you what I've tried... I've found this code on the internet and I've modified it, and is a little buggy so if you can fix it too I would be awesome ahahah! 我给了我我尝试过的东西...我已经在互联网上找到了这段代码,并且已经对其进行了修改,并且有点小虫子,所以如果您也可以修复它,我会很棒的啊哈哈!

public static void main(String[] args) {
    char t[]="0123456789abcdef".toCharArray();
    int i = 0;

    while(i<1000) {
            System.out.println("Char: " + t[i/16] + t[i%16] + " ==> " + "\u00" + t[i/16] + t[i++%16]);
    }
}

PS: sometimes randomly, the compiler give me this error: PS:有时是随机的,编译器给我这个错误:

at unicodetest.UnicodeTest.main(Char: 83 ==> 83
UnicodeTest.java:10)

The line 10 is: 第10行是:

System.out.println("Char: " + t[i/16] + t[i%16] + " ==> " + t[i/16] + t[i++%16]);

EDIT: It should appear like this: 编辑:它应该看起来像这样:

Char: 20 ==> ' '
Char: 21 ==> '!'
Char: 22 ==> '"'
Char: 23 ==> '#'
Char: 24 ==> '$'
Char: 25 ==> '%'
Char: 26 ==> '&'
Char: 27 ==> '''
Char: 28 ==> '('
Char: 29 ==> ')'
Char: 2a ==> '*'
Char: 2b ==> '+'
Char: 2c ==> ','
Char: 2d ==> '-'
Char: 2e ==> '.'
Char: 2f ==> '/'
Char: 30 ==> '0'
Char: 31 ==> '1'
Char: 32 ==> '2'
Char: 33 ==> '3'
Char: 34 ==> '4'
Char: 35 ==> '5'
Char: 36 ==> '6'
Char: 37 ==> '7'
Char: 38 ==> '8'
Char: 39 ==> '9'
Char: 3a ==> ':'
Char: 3b ==> ';'
Char: 3c ==> '<'
Char: 3d ==> '='
Char: 3e ==> '>'
Char: 3f ==> '?'
Char: 40 ==> '@'
Char: 41 ==> 'A'
Char: 42 ==> 'B'
Char: 43 ==> 'C'
Char: 44 ==> 'D'
Char: 45 ==> 'E'
Char: 46 ==> 'F'
Char: 47 ==> 'G'
...
Char: 37b ==> 'ͻ'
Char: 37c ==> 'ͼ'
Char: 37d ==> 'ͽ'
...
Char: 3e4 ==> 'Ϥ'
Char: 3e5 ==> 'ϥ'
Char: 3e6 ==> 'Ϧ'
Char: 3e7 ==> 'ϧ'

Use Character.toChars(int) to convert from a code point to representing characters. 使用Character.toChars(int)从代码点转换为代表字符。 Below will print all UTF characters between U+0000 and U+03E8, first 1000 code points. 下面将打印U + 0000和U + 03E8之间的所有UTF字符,前1000个代码点。

public static void main(String[] args) {
  IntStream.range(0, 1000)
    .mapToObj(i -> "Char: " + i + " ==> " + new String(Character.toChars(i)))
    .forEach(System.out::println);
}

Here's a slightly different answer, which doesn't use streams. 这是一个略有不同的答案,它不使用流。 You should be able to modify it to produce any output you like.; 您应该能够对其进行修改以产生所需的任何输出。

I'm using a couple of tricks. 我正在使用一些技巧。 The first is that char is an integer and can promote to an int . 首先是char是一个整数,可以提升为int So to initialize the counter I actually assign it the first ASCII character, which is ' ' (a space). 因此,要初始化计数器,我实际上为它分配了第一个ASCII字符,即' ' (一个空格)。 The first 32 ASCII characters are control characters and don't print, so I just skip them. 前32个ASCII字符是控制字符,不会打印,因此我将其跳过。

The second is that instead of calling Character.toChars() I just cast to an int to char . 第二个是,我没有将Character.toChars()调用,而是将其int转换为intchar This works fine as long as you control the input fully and there's no range errors. 只要您完全控制输入并且没有范围错误,此方法就可以正常工作。

   public static void main( String[] args ) {
      for( int i = ' '; i < 1000; i++ ) 
         System.out.println( "Char: " + Integer.toHexString( i ) 
             + " ==> " + (char)i );
   }

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

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