Java 从其编号创建Unicode字符
我想在Java中显示Unicode字符。如果我这样做,效果很好:Java 从其编号创建Unicode字符,java,string,unicode,character,Java,String,Unicode,Character,我想在Java中显示Unicode字符。如果我这样做,效果很好: String symbol=“\u2202” 符号等于“∂". 这就是我想要的 问题是我知道Unicode数字,需要从中创建Unicode符号。我(对我)尝试了一件显而易见的事情: int c = 2202; String symbol = "\\u" + c; 但是,在本例中,符号等于“\u2202”。这不是我想要的 如果我知道符号的Unicode编号(但只能在运行时——我不能像第一个示例那样硬编码),如何构造符号?请记住
String symbol=“\u2202”代码>
符号等于“∂". 这就是我想要的
问题是我知道Unicode数字,需要从中创建Unicode符号。我(对我)尝试了一件显而易见的事情:
int c = 2202;
String symbol = "\\u" + c;
但是,在本例中,符号等于“\u2202”。这不是我想要的
如果我知道符号的Unicode编号(但只能在运行时——我不能像第一个示例那样硬编码),如何构造符号?请记住,char
是一种整数类型,因此可以指定一个整数值和一个char常量
char c = 0x2202;//aka 8706 in decimal. \u codepoints are in hex.
String s = String.valueOf(c);
只需将您的int
转换为char
。您可以使用Character.toString()将其转换为String
:
编辑:
请记住,Java源代码中的转义序列(位)是十六进制的,因此,如果您试图复制转义序列,您将需要类似于int c=0x2202
的代码:
int cc = 0x2202;
char ccc = (char) Integer.parseInt(String.valueOf(cc), 16);
final String text = String.valueOf(ccc);
是由Arne Vajhøj.编写的。如果您想将UTF-16编码的代码单元作为字符
,您可以按照其他人的建议解析整数并转换为它
如果要支持所有代码点,请使用。这将处理代码点无法放入单个char
值的情况
医生说:
将指定的字符(Unicode代码点)转换为存储在字符数组中的UTF-16表示形式。如果指定的代码点是BMP(基本多语言平面或平面0)值,则生成的字符数组具有与代码点相同的值。如果指定的代码点是补充代码点,则生成的字符数组具有相应的代理项对
这里的其他答案要么只支持unicode,最多支持U+FFFF(答案只处理一个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
(答案是在DOT NET 4.5和java中,必须存在类似的方法)
我来自印度的西孟加拉邦。
据我所知,你的问题是。。。
你想生产类似于অ ' (这是一封孟加拉语的信)
其中包含Unicode十六进制:0X0985
现在,如果您知道关于您的语言的这个值,那么您将如何正确地生成特定于语言的Unicode符号
在Dot Net中,它非常简单:
int c = 0X0985;
string x = Char.ConvertFromUtf32(c);
现在x是你的答案。
但这是一个十六进制的转换,句子到句子的转换是研究人员的工作:P这一个对我来说很好
String cc2 = "2202";
String text2 = String.valueOf(Character.toChars(Integer.parseInt(cc2, 16)));
现在text2将拥有∂. 不幸的是,要消除第一条评论(newbiedoodle)中提到的一个反冲并不能带来好的结果。大多数(如果不是全部)IDE都会出现语法错误。原因在于Java转义Unicode格式需要语法“\uxxx”“,其中XXXX是4个十六进制数字,这是必需的。尝试从碎片中折叠此字符串失败。当然,“\u”与“\\u”不同。第一个语法表示逃逸的“u”,第二个表示逃逸的反冲(即反冲),后跟“u”。奇怪的是,Apache页面上出现了一个实用程序,它正是这样做的。但事实上,确实如此。Apache有一些自己的实用程序(我没有测试它们),它们为您完成这项工作。也许,这仍然不是你想要的。但是这个实用程序有很好的解决方案。使用上述组合(MeraNaamJoker)。我的解决方案是创建这个转义模拟字符串,然后将其转换回unicode(以避免真正的转义unicode限制)。我用它来复制文本,所以有可能在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;
}
/**
*将字符转换为模拟unicode格式,即“\\u0020”。
*
*此格式是Java源代码格式。
*
*CharUtils.unicodescaped(“”)=“\\u0020”
*CharUtils.unicodescaped('A')=“\\u0041”
*
*@param ch要转换的字符
*@return模仿转义的unicode字符串,
*/
公共静态字符串unicodescaped(char-ch){
字符串返回str;
//字符串uniTemplate=“\u0000”;
最终静态字符串charEsc=“\\u”;
if(ch<0x10){
returnStr=“000”+整数.tohextstring(ch);
}
否则如果(ch<0x100){
returnStr=“00”+整数.tohextstring(ch);
}
否则如果(ch<0x1000){
returnStr=“0”+整数.tohextstring(ch);
}
其他的
returnStr=“”+整数.tohextstring(ch);
return charEsc+returnStr;
}
/**
*将字符串从UTF8转换为模拟unicode格式,即“\\u0020”。
*注意:我不能使用真正的unicode格式,因为这是立即翻译的
*在编译和编辑器(即netbeans)检查字符时
*而不是真正的unicode格式,即使用模拟unicode格式“\\u0020”的“\u0020”i
*作为一个字符串,但它不会给出相同的结果,当然
*
*此格式是Java源代码格式。
*
*CharUtils.unicodescaped(“”)=“\\u0020”
*CharUtils.unicodescaped('A')=“\\u0041”
*
*@param String-要转换的UTF8字符串中的nationalString
*@return是JAVA unicode格式的字符串
*/
公共字符串encodeStr(String nationalString)引发不支持的编码异常{
字符串convertedString=“”;
对于(int i=0;i /**
* 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;
}
// 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
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();
}
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'.
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
int codePoint = '\u2202';
String s = Character.toString(codePoint); // <<< Requires JDK 11 !!!
System.out.println(s); // Prints ∂