且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将Unicode值转换为字符?

更新时间:2023-02-14 11:32:02

20B9是Unicode点值. IE. U + 20B9.它不是经过编码的,也不是UTF-8.

20B9 is a Unicode point value. I.e. U+20B9. It's not encoded and not UTF-8.

尝试currencySymbol.getBytes("UTF-8")是毫无意义的,因为您的字符串已经从字节中解码出来了-您将得到的只是十六进制字符串的ASCII字节,因此您将获得响应.

It's pointless trying to currencySymbol.getBytes("UTF-8") as your string has already been decoded from bytes - all you will get is the ASCII bytes of the string of the hex, hence the response you're getting.

相反,您需要:

  1. 将十六进制转换为代表Unicode代码点的整数
  2. 在给定的Unicode代码点获取字符

示例:

 int codepoint = Integer.parseInt(currency.getSymbol(), 16);
 char[] currencySymbol =Character.toChars(codepoint);