Pobierz wartość unicode znaku

Czy Jest jakiś sposób w Javie, abym mógł uzyskać odpowiednik Unicode dowolnego znaku? np.

Załóżmy metodę getUnicode(char c). Wywołanie getUnicode('÷') powinno zwrócić \u00f7.

Author: Andrew Thompson, 2010-02-08

7 answers

Możesz to zrobić dla dowolnego znaku Java używając one liner tutaj:

System.out.println( "\\u" + Integer.toHexString('÷' | 0x10000).substring(1) );

Ale to będzie działać tylko dla znaków Unicode aż do Unicode 3.0, dlatego precedens można to zrobić dla dowolnego Java char.

Ponieważ Java została zaprojektowana na długo przed pojawieniem się Unicode 3.1 i stąd jej prymitywny znak jest nieodpowiedni do reprezentowania Unicode 3.1 i nowszych: nie ma już mapowania "jeden znak Unicode na jeden znak Java " (zamiast tego używa się potwornego hack'a).

Więc naprawdę masz aby sprawdzić swoje wymagania tutaj: czy musisz obsługiwać Java char lub dowolny możliwy znak Unicode?

 51
Author: SyntaxT3rr0r,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-08 09:13:01

Jeśli masz Javę 5, użyj char c = ...; String s = String.format ("\\u%04x", (int)c);

Jeśli Twoje źródło nie jest znakiem Unicode (char), ale ciągiem znaków, musisz użyć charAt(index), aby uzyskać znak Unicode na pozycji index.

Nie używaj codePointAt(index), ponieważ zwróci to 24-bitowe wartości (pełny Unicode), które nie mogą być reprezentowane tylko przez 4 cyfry szesnastkowe (potrzebuje 6). Zobacz dokumenty dla wyjaśnienia .

[Edytuj] aby było jasne: ta odpowiedź nie używa Unicode, ale metody używanej przez Javę do reprezentowania znaków Unicode (tj. pary zastępcze), ponieważ char jest 16-bitowy, a Unicode 24-bitowy. Pytanie powinno brzmieć: "Jak mogę przekonwertować char na 4-cyfrowy numer hex", ponieważ nie chodzi (tak naprawdę) o Unicode.

 33
Author: Aaron Digulla,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-08 12:30:32
private static String toUnicode(char ch) {
    return String.format("\\u%04x", (int) ch);
}
 10
Author: Yogesh Dubey,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2013-08-07 08:20:01
char c = 'a';
String a = Integer.toHexString(c); // gives you---> a = "61"
 5
Author: Deepak Sharma,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2017-02-01 12:50:08

Znalazłem ten fajny kod w sieci.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Unicode {

public static void main(String[] args) {
System.out.println("Use CTRL+C to quite to program.");

// Create the reader for reading in the text typed in the console. 
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);

try {
  String line = null;
  while ((line = bufferedReader.readLine()).length() > 0) {
    for (int index = 0; index < line.length(); index++) {

      // Convert the integer to a hexadecimal code.
      String hexCode = Integer.toHexString(line.codePointAt(index)).toUpperCase();


      // but the it must be a four number value.
      String hexCodeWithAllLeadingZeros = "0000" + hexCode;
      String hexCodeWithLeadingZeros = hexCodeWithAllLeadingZeros.substring(hexCodeWithAllLeadingZeros.length()-4);

      System.out.println("\\u" + hexCodeWithLeadingZeros);
    }

  }
} catch (IOException ioException) {
       ioException.printStackTrace();
  }
 }
}

Oryginalny Artykuł

 0
Author: Chathuranga Chandrasekara,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-02-08 08:45:11

Czy jesteś wybredny z używaniem Unicode, ponieważ w Javie jest to prostsze, jeśli piszesz swój program, aby używał wartości " dec " lub (kod HTML), możesz po prostu rzucać typy danych między char i int

char a = 98;
char b = 'b';
char c = (char) (b+0002);

System.out.println(a);
System.out.println((int)b);
System.out.println((int)c);
System.out.println(c);

Daje to wyjście

b
98
100
d
 0
Author: Jordan Doerksen,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-02-26 03:33:59

Po pierwsze, dostaję wysoką stronę char. Potem złap za dół. Konwertuj wszystkie rzeczy w łańcuchu Heksowym i umieść prefiks.

int hs = (int) c  >> 8;
int ls = hs & 0x000F;

String highSide = Integer.toHexString(hs);
String lowSide = Integer.toHexString(ls);
lowSide = Integer.toHexString(hs & 0x00F0);
String hexa = Integer.toHexString( (int) c );

System.out.println(c+" = "+"\\u"+highSide+lowSide+hexa);
 0
Author: Josiel Novaes,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2015-04-12 22:13:31