从它的编号中创buildUnicode字符

我想在Java中显示一个Unicode字符。 如果我这样做,它工作得很好:

String symbol = "\u2202";

符号等于“∂”。 这就是我想要的。

问题是我知道Unicode编号,需要从中创buildUnicode符号。 我试过(对我来说)显而易见的事情:

 int c = 2202; String symbol = "\\u" + c; 

但在这种情况下,符号等于“\ u2202”。 这不是我想要的。

如果我知道它的Unicode号码(但只在运行时—我不能像第一个例子那样对它进行硬编码),我该如何构造符号?

只需将您的int转换为char 。 您可以使用Character.toString()将其转换为String

 String s = Character.toString((char)c); 

编辑:

只要记住Java源代码中的转义序列( \u位)是hex的,所以如果你想重现一个转义序列,你需要像int c = 0x2202这样的东西。

如果你想获得一个UTF-16编码的代码单元作为char ,你可以parsing整数,并像其他人所build议的那样投射。

如果你想支持所有的代码点,使用Character.toChars(int) 。 这将处理代码点不适合单个char值的情况。

Doc说:

将指定的字符(Unicode代码点)转换为存储在char数组中的UTF-16表示forms。 如果指定的代码点是BMP(Basic Multilingual Plane或Plane 0)值,则生成的char数组与codePoint具有相同的值。 如果指定的代码点是一个补充代码点,则生成的char数组具有相应的代理对。

这里的其他答案要么只支持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)); 

我也做了一个快速的testing,以确定哪些转换方法有效,哪些不转换

 int codePoint = 128149; char[] charPair = Character.toChars(codePoint); String str = new String(charPair, 0, 2); System.out.println("First code point: " + str.codePointAt(0)); // 128149, worked String str2 = charPair.toString(); System.out.println("Second code point: " + str2.codePointAt(0)); // 91, didn't work String str3 = new String(charPair); System.out.println("Third code point: " + str3.codePointAt(0)); // 128149, worked String str4 = String.valueOf(code); System.out.println("Fourth code point: " + str4.codePointAt(0)); // 49, didn't work String str5 = new String(new int[] {codePoint}, 0, 1); System.out.println("Fifth code point: " + str5.codePointAt(0)); // 128149, worked 

请记住, char是一个整型,因此可以给定一个整数值,以及一个char常量。

 char c = 0x2202;//aka 8706 in decimal. \u codepoints are in hex. String s = String.valueOf(c); 

这个对我来说工作得很好。

  String cc2 = "2202"; String text2 = String.valueOf(Character.toChars(Integer.parseInt(cc2, 16))); 

现在text2将有∂。

这是你如何做到的:

 int cc = 0x2202; char ccc = (char) Integer.parseInt(String.valueOf(cc), 16); final String text = String.valueOf(ccc); 

这个解决scheme是由ArneVajhøj。

下面的代码将用日语写出4个unicode字符(用小数表示),用于单词“be”。 是的,日语中的动词“be”有4个字符! 字符的值是十进制的,它已被读入一个String []的数组中 – 例如使用split。 如果你有八进制或hex, parseInt也是一个基数。

 // 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 

不幸的是,要消除第一条评论(newbiedoodle)中提到的一个反弹,并不会带来好的结果。 大多数(如果不是全部的话)IDE发出语法错误。 原因在于,Java Escaped Unicode格式需要语法“\ uXXXX”,其中XXXX是4位hex数字,这是强制性的。 尝试折叠这个string从失败。 当然,“\ u”与“\ u”不一样。 第一个语法意味着逃脱'你',第二意味着逃脱反弹(这是反冲),其次是'你'。 奇怪的是,在Apache页面上显示的是实用程序,它完全是这样的行为。 但实际上,这是逃生模仿效用 。 Apache有一些它自己的工具(我没有testet他们),这为你做这个工作。 也许,那还不是,你想要什么。 Apache转义Unicode实用程序但是这个实用程序1有很好的解决scheme。 与上述组合(MeraNaamJoker)。 我的解决scheme是创build这个Escaped mimicstring,然后将其转换回unicode(以避免真正的Escaped Unicode限制)。 我用它来复制文本,所以有可能在uencode方法中使用'\\ u'会比'\\\\ u'更好。 尝试一下。

  /** * Converts character to the mimic unicode format ie '\\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 ie '\\u0020'. * notice: i cannot use real unicode format, because this is immediately translated * to the character in time of compiling and editor (ie netbeans) checking it * instead reaal unicode format ie '\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 ie '\\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; } 
 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'. 

char c =(char)0x2202; String s =“”+ c;

这里是一个块打印出\u00c0\u00ff之间的Unicode字符:

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

(答案是在NET 4.5和Java中,必须有类似的方法存在)

我来自印度西孟加拉邦。 据我的理解你的问题是…你想产生类似于'অ'(这是一个字母在孟加拉语)有Unicodehex: 0X0985

现在如果你知道你的语言的这个价值,那么你将如何产生该语言特定的Unicode符号?

在DotNet中,它就像这样简单:

 int c = 0X0985; string x = Char.ConvertFromUtf32(c); 

现在x是你的答案。 但是这是HEX由HEX转换而来的,句子转换是一个研究者的工作:P