2017-04-05 2 views
1

Java에서 단일 유니 코드 문자를 나타내고 싶습니다. 어떤 원시 또는 클래스에 적합합니까?Java에서 단일 유니 코드 문자를 어떻게 표현해야합니까?

2 바이트 char에 비해 너무 큰 유니 코드 문자를 저장할 수 있기를 바랍니다.

+1

'char' – prasanth

+0

'char ch = '\ u1111'; –

+2

@prasanth :'char'는 모든 유니 코드 문자를 담을 수 없습니다. 'char'는 기본적으로 UTF-16 코드 단위에 해당합니다. –

답변

9

char은 실제로 16 비트, a char corresponds to a UTF-16 code unit입니다. 단일 UTF-16 코드 단위 (예 : Emojis)에 맞지 않는 문자는 두 개의 char을 필요로합니다.

어떤 이유로 든 개별적으로 저장해야하는 경우 int을 사용할 수 있습니다. 현재 유니 코드로 허용 된 모든 0x10FFFF 코드 포인트에 대해 충분한 공간이 있습니다 (그리고 나서 일부 공간). 이것이 JDK에서 사용하는 것입니다 (예 : Character.codePointAt(CharSequence seq, int index)String(int[] codePoints, int offset, int count)).

무상 변환 예 (live on ideone) :

String s = ""; 
int emoji = Character.codePointAt(s, 0); 
String unumber = "U+" + Integer.toHexString(emoji).toUpperCase(); 
System.out.println(s + " is code point " + unumber); 
String s2 = new String(new int[] { emoji }, 0, 1); 
System.out.println("Code point " + unumber + " converted back to string: " + s2); 
System.out.println("Successful round-trip? " + s.equals(s2)); 

출력 :

 
    is code point U+1F602 
Code point U+1F602 converted back to string: 
Successful round-trip? true 
1

는 문자의 정의에 따라 달라집니다 : 당신은 하나의 유니 코드 코드 포인트를 의미하는 경우

int을 사용하십시오.이 값은 U + 0000에서 U + 1FFFFF까지의 모든 값을 유지할 수 있습니다.

그러나 한 문자로 나타나는 내용은 여러 코드 포인트를 차지합니다. 이것은 특히 이모티콘 (emoji)과 공통적입니다.

가장 논리적 인 방법으로 저장하려면 String을 사용하십시오.

+0

** # EmojiCodeSheet ** [여기] (https://github.com/shanraisshan/EmojiCodeSheet)에서 모든 그림 이모티콘 목록을 string/int 형식으로 찾을 수 있습니다. – shanraisshan

관련 문제