简体   繁体   中英

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.

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! ("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:

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

The line 10 is:

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. Below will print all UTF characters between U+0000 and U+03E8, first 1000 code points.

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 . So to initialize the counter I actually assign it the first ASCII character, which is ' ' (a space). The first 32 ASCII characters are control characters and don't print, so I just skip them.

The second is that instead of calling Character.toChars() I just cast to an int to char . 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 );
   }

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