Creating Unicode character from its number

后端 未结 13 1803
挽巷
挽巷 2020-11-28 21:38

I want to display a Unicode character in Java. If I do this, it works just fine:

String symbol = \"\\u2202\";

symbol is equal to \"∂\". That\'

13条回答
  •  醉梦人生
    2020-11-28 22:05

    The code below will write the 4 unicode chars (represented by decimals) for the word "be" in Japanese. Yes, the verb "be" in Japanese has 4 chars! The value of characters is in decimal and it has been read into an array of String[] -- using split for instance. If you have Octal or Hex, parseInt take a radix as well.

    // pseudo code
    // 1. init the String[] containing the 4 unicodes in decima :: intsInStrs 
    // 2. allocate the proper number of character pairs :: c2s
    // 3. Using Integer.parseInt (... with radix or not) get the right int value
    // 4. place it in the correct location of in the array of character pairs
    // 5. convert c2s[] to String
    // 6. print 
    
    String[] intsInStrs = {"12354", "12426", "12414", "12377"}; // 1.
    char [] c2s = new char [intsInStrs.length * 2];  // 2.  two chars per unicode
    
    int ii = 0;
    for (String intString : intsInStrs) {
        // 3. NB ii*2 because the 16 bit value of Unicode is written in 2 chars
        Character.toChars(Integer.parseInt(intsInStrs[ii]), c2s, ii * 2 ); // 3 + 4
        ++ii; // advance to the next char
    }
    
    String symbols = new String(c2s);  // 5.
    System.out.println("\nLooooonger code point: " + symbols); // 6.
    // I tested it in Eclipse and Java 7 and it works.  Enjoy
    

提交回复
热议问题