숫자에서 유니코드 문자 생성
자바에서 유니코드 문자를 표시하고 싶다.이렇게 하면 정상적으로 동작합니다.
String symbol = "\u2202";
기호는 "symbol"과 같습니다.그게 내가 원하는 거야.
문제는 유니코드 번호를 알고 있기 때문에 유니코드 기호를 작성해야 한다는 것입니다.나는 (나에게) 당연한 것을 시도했다.
int c = 2202;
String symbol = "\\u" + c;
단, 이 경우 기호는 "\u2202"와 같습니다.그건 내가 원하는 게 아니야.
유니코드 번호를 알고 있으면 어떻게 심볼을 구성할 수 있습니까(실시간에만 - 첫 번째 예시와 같이 하드코드로 입력할 수 없습니다.
을 UTF-16 char
다른 사용자가 제안한 대로 정수를 해석하여 캐스트할 수 있습니다.
모든 코드 포인트를 지원하려면 를 사용합니다.코드 포인트가 단일에 들어갈 수 없는 경우를 처리합니다.char
discloss.discloss 。
의사:
지정된 문자(Unicode 코드 포인트)를 문자 배열에 저장된 UTF-16 표현으로 변환합니다.지정된 코드 포인트가 BMP(Basic Multilinguage Plane or Plane 0) 값인 경우 결과 char 배열은 codePoint와 동일한 값을 가집니다.지정된 코드 포인트가 보조 코드 포인트인 경우 결과 char 배열은 대응하는 서로게이트 쌍을 가집니다.
해 주세요.int
a까지char
을 .로 할 수 .String
를 사용합니다.Character.toString()
:
String s = Character.toString((char)c);
편집:
코드(Java)의(Sequences)는,\u
16진수이기 시퀀스를하려면 16진수 16진수 int c = 0x2202
.
다른 답변은 U+FFFF까지 Unicode를 지원하거나(한 개의 char 인스턴스를 처리하는 답변) 실제 기호로 이동하는 방법을 알려주지 않습니다(Character.toChars()에서 멈추거나 잘못된 메서드를 사용함). 이 답변도 여기에 추가합니다.
보조 코드 포인트를 지원하려면 다음과 같이 해야 합니다.
// this character:
// http://www.isthisthingon.org/unicode/index.php?page=1F&subpage=4&glyph=1F495
// using code points here, not U+n notation
// for equivalence with U+n, below would be 0xnnnn
int codePoint = 128149;
// converting to char[] pair
char[] charPair = Character.toChars(codePoint);
// and to String, containing the character we want
String symbol = new String(charPair);
// we now have str with the desired character as the first item
// confirm that we indeed have character with code point 128149
System.out.println("First code point: " + symbol.codePointAt(0));
또, 어떤 변환 방법이 유효하고, 어떤 변환 방법이 무효인지에 대해서도 간단한 테스트를 실시했습니다.
int codePoint = 128149;
char[] charPair = Character.toChars(codePoint);
System.out.println(new String(charPair, 0, 2).codePointAt(0)); // 128149, worked
System.out.println(charPair.toString().codePointAt(0)); // 91, didn't work
System.out.println(new String(charPair).codePointAt(0)); // 128149, worked
System.out.println(String.valueOf(codePoint).codePointAt(0)); // 49, didn't work
System.out.println(new String(new int[] {codePoint}, 0, 1).codePointAt(0));
// 128149, worked
--
주의: 코멘트에서 @Axel이 언급했듯이 Java 11에는 작업에 가장 적합한 Character.toString(int codePoint)이 있습니다.
이건 나한테 잘 먹혔어.
String cc2 = "2202";
String text2 = String.valueOf(Character.toChars(Integer.parseInt(cc2, 16)));
여기서 text2에는 ∂이 붙습니다.
하세요.char
문자 상수
char c = 0x2202;//aka 8706 in decimal. \u codepoints are in hex.
String s = String.valueOf(c);
String st="2202";
int cp=Integer.parseInt(st,16);// it convert st into hex number.
char c[]=Character.toChars(cp);
System.out.println(c);// its display the character corresponding to '\u2202'.
이것은 오래된 질문이지만, 오늘 출시된 Java 11에서는 매우 간단한 방법이 있습니다.Character.toString()의 새로운 오버로드를 사용할 수 있습니다.
public static String toString(int codePoint)
Returns a String object representing the specified character (Unicode code point). The result is a string of length 1 or 2, consisting solely of the specified codePoint.
Parameters:
codePoint - the codePoint to be converted
Returns:
the string representation of the specified codePoint
Throws:
IllegalArgumentException - if the specified codePoint is not a valid Unicode code point.
Since:
11
이 메서드는 모든 Unicode 코드 포인트를 지원하므로 반환되는 String의 길이가 반드시 1은 아닙니다.
질문의 예에 필요한 코드는 다음과 같습니다.
int codePoint = '\u2202';
String s = Character.toString(codePoint); // <<< Requires JDK 11 !!!
System.out.println(s); // Prints ∂
이 방법에는 다음과 같은 이점이 있습니다.
- Unicode를 하여 할 수 뿐만 아니라 Unicode 합니다.
char
- 간결하고 코드가 무엇을 하는지 이해하기 쉽습니다.
- 할 때는 값이 문자열이 됩니다.
char[]
그게 네가 원하는 거야코드 포인트를 다음과 같이 반환하려면 McDowell이 게시한 답변이 적절합니다.char[]
.
방법은 다음과 같습니다.
int cc = 0x2202;
char ccc = (char) Integer.parseInt(String.valueOf(cc), 16);
final String text = String.valueOf(ccc);
이 솔루션은 Arne Vajhöj에 의해 작성되었습니다.
아래 코드는 일본어 'be'의 4개의 유니코드 문자(소수로 표시됨)를 씁니다.네, 일본어 동사 be는 4자예요!문자 값은 10진수이며 String [] 배열로 읽혀졌습니다(예: 분할 사용).Octal 또는 Hex가 있는 경우 해석기수 또한 취한다.
// 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
to here here 니 、 유 、 char 、 char 、 char 、 char 、 char 、 char 、 char 、 char 、 char 、 char 、 char 、 char 、 char char 。\u00c0
로로 합니다.\u00ff
:
char[] ca = {'\u00c0'};
for (int i = 0; i < 4; i++) {
for (int j = 0; j < 16; j++) {
String sc = new String(ca);
System.out.print(sc + " ");
ca[0]++;
}
System.out.println();
}
유감스럽게도 첫 번째 댓글(뉴욕)에서 언급한 것처럼 한 번의 반발을 제거하는 것은 좋은 결과로 이어지지 않는다.(전부는 아니지만) 대부분의 IDE에서 구문 오류가 발생.그 이유는 Java Escape Unicode 형식에서 구문 "\uXXXX"가 필요하기 때문입니다.여기서 XXXX는 필수 4자리 16진수입니다.이 문자열을 조각에서 접으려고 하면 실패합니다.물론 "\u"는 "\u"와 같지 않습니다.첫 번째 구문은 이스케이프된 'u'를 의미하고, 두 번째 구문은 이스케이프된 백래시(백래시)를 의미하며, 그 다음 'u'를 의미합니다.이상하게도 Apache 페이지에는 바로 이 동작을 하는 유틸리티가 표시됩니다.하지만 실제로는 이스케이프 모방 유틸리티입니다.Apache에는 테스트를 거치지 않은 자체 유틸리티가 몇 개 있어 이 작업을 수행할 수 있습니다.여전히 당신이 원하는 것은 아닐지도 모른다.Apache Escape Unicode 유틸리티 그러나 이 유틸리티 1은 솔루션에 대한 좋은 접근 방식을 제공합니다.상기 조합으로 (Mera Naam)이 이스케이프된 모방 문자열을 작성한 후 유니코드로 변환합니다(실제 이스케이프된 유니코드 제한을 피하기 위해).텍스트 복사용으로 사용했기 때문에 uencode 방식에서는 "\\u"를 제외하고 "\u"를 사용하는 것이 좋습니다.먹어봐.
/**
* Converts character to the mimic unicode format i.e. '\\u0020'.
*
* This format is the Java source code format.
*
* CharUtils.unicodeEscaped(' ') = "\\u0020"
* CharUtils.unicodeEscaped('A') = "\\u0041"
*
* @param ch the character to convert
* @return is in the mimic of escaped unicode string,
*/
public static String unicodeEscaped(char ch) {
String returnStr;
//String uniTemplate = "\u0000";
final static String charEsc = "\\u";
if (ch < 0x10) {
returnStr = "000" + Integer.toHexString(ch);
}
else if (ch < 0x100) {
returnStr = "00" + Integer.toHexString(ch);
}
else if (ch < 0x1000) {
returnStr = "0" + Integer.toHexString(ch);
}
else
returnStr = "" + Integer.toHexString(ch);
return charEsc + returnStr;
}
/**
* Converts the string from UTF8 to mimic unicode format i.e. '\\u0020'.
* notice: i cannot use real unicode format, because this is immediately translated
* to the character in time of compiling and editor (i.e. netbeans) checking it
* instead reaal unicode format i.e. '\u0020' i using mimic unicode format '\\u0020'
* as a string, but it doesn't gives the same results, of course
*
* This format is the Java source code format.
*
* CharUtils.unicodeEscaped(' ') = "\\u0020"
* CharUtils.unicodeEscaped('A') = "\\u0041"
*
* @param String - nationalString in the UTF8 string to convert
* @return is the string in JAVA unicode mimic escaped
*/
public String encodeStr(String nationalString) throws UnsupportedEncodingException {
String convertedString = "";
for (int i = 0; i < nationalString.length(); i++) {
Character chs = nationalString.charAt(i);
convertedString += unicodeEscaped(chs);
}
return convertedString;
}
/**
* Converts the string from mimic unicode format i.e. '\\u0020' back to UTF8.
*
* This format is the Java source code format.
*
* CharUtils.unicodeEscaped(' ') = "\\u0020"
* CharUtils.unicodeEscaped('A') = "\\u0041"
*
* @param String - nationalString in the JAVA unicode mimic escaped
* @return is the string in UTF8 string
*/
public String uencodeStr(String escapedString) throws UnsupportedEncodingException {
String convertedString = "";
String[] arrStr = escapedString.split("\\\\u");
String str, istr;
for (int i = 1; i < arrStr.length; i++) {
str = arrStr[i];
if (!str.isEmpty()) {
Integer iI = Integer.parseInt(str, 16);
char[] chaCha = Character.toChars(iI);
convertedString += String.valueOf(chaCha);
}
}
return convertedString;
}
char c=(char)0x2202; 문자열 s="+c;
(정답은 DOT NET 4.5에 있으며 Java에서도 유사한 접근법이 존재해야 합니다.)
저는 인도의 웨스트 벵골 출신입니다.제가 알기로는 당신의 문제인 것으로 알고 있습니다.Unicode HEX를 가진 ' 벵골어 문자'와 같은 것을 생성하려고 합니다.0X0985
.
만약 당신이 당신의 언어와 관련하여 이 값을 안다면, 당신은 어떻게 그 언어 고유의 유니코드 기호를 만들 것입니까?
Dot Net에서는 다음과 같이 간단합니다.
int c = 0X0985;
string x = Char.ConvertFromUtf32(c);
x가 정답입니다.그러나 이것은 HEX by HEX 변환이며 문장에서 문장으로의 변환은 연구자들을 위한 작업이다.p
언급URL : https://stackoverflow.com/questions/5585919/creating-unicode-character-from-its-number
'source' 카테고리의 다른 글
Java에 Serializable 인터페이스가 필요한 이유 (0) | 2022.09.03 |
---|---|
Jackson 2.2의 Object Mapper에서 JSON을 예쁘게 인쇄했습니다. (0) | 2022.09.03 |
POST 후에 REST로 콘텐츠를 반송해도 될까요? (0) | 2022.09.03 |
왜 "a"가 C로!= "a"가 되는 거죠? (0) | 2022.09.01 |
웹팩/Grails/Gradle 프로젝트에 서드파티 JSlibs 추가 (0) | 2022.09.01 |