Program Tip

문자의 유니 코드 값 가져 오기

programtip 2020. 11. 11. 20:33
반응형

문자의 유니 코드 값 가져 오기


어떤 문자에 해당하는 유니 코드를 얻을 수 있도록 Java에 방법이 있습니까? 예 :

방법을 가정하십시오 getUnicode(char c). 호출 getUnicode('÷')이 반환되어야합니다 \u00f7.


여기에서 하나의 라이너를 사용하여 모든 Java 문자에 대해 수행 할 수 있습니다.

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

그러나 그것은 유니 코드 3.0까지의 유니 코드 문자에 대해서만 작동 할 것입니다. 그래서 어떤 Java 문자에 대해서도 그렇게 할 수 있다고 정확하게했습니다.

자바는 유니 코드 3.1이 나오기 전에 설계 되었기 때문에 자바의 문자 프리미티브는 유니 코드 3.1 이상을 표현하기에 부적절합니다. "하나의 유니 코드 문자 대 하나의 Java 문자"매핑이 더 이상 없습니다 (거대한 해킹이 사용되는 대신).

따라서 여기에서 요구 사항을 확인해야합니다. Java char 또는 가능한 유니 코드 문자를 지원해야합니까?


Java 5가있는 경우 char c = ...; String s = String.format ("\\u%04x", (int)c);

소스가 유니 코드 문자 ( char)가 아니라 문자열 인 경우을 사용 charAt(index)하여 위치에서 유니 코드 문자를 가져와야 index합니다.

codePointAt(index)4 개의 16 진수 (6 개 필요)로 표현할 수없는 24 비트 값 (전체 유니 코드)을 반환하므로 사용하지 마십시오 . 설명은 문서를 참조하십시오 .

[편집] 명확하게하기 위해 :이 대답은 유니 코드를 사용하지 않고 자바가 유니 코드 문자 (즉, 대리 쌍)를 표현하는 데 사용하는 방법입니다. char는 16 비트이고 유니 코드는 24 비트이기 때문입니다. 문제는 "어떻게 char4 자리 16 진수 로 변환 할 수 있는가"여야합니다. 유니 코드에 관한 것이 아니기 때문입니다.


private static String toUnicode(char ch) {
    return String.format("\\u%04x", (int) ch);
}

char c = 'a';
String a = Integer.toHexString(c); // gives you---> a = "61"

웹에서이 멋진 코드를 찾았습니다.

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();
  }
 }
}

원본 기사


are you picky with using Unicode because with java its more simple if you write your program to use "dec" value or (HTML-Code) then you can simply cast data types between char and 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);

Gives this output

b
98
100
d

First, I get the high side of the char. After, get the low side. Convert all of things in HexString and put the prefix.

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);

참고URL : https://stackoverflow.com/questions/2220366/get-unicode-value-of-a-character

반응형