当我运行以下语句时,
System.out.println("Character is "+(char)65536); //equivalent to 0x10000
我得到的输出为
Character is
如何描述它?
最佳答案
Character
类提供了以下方法
Character.highSurrogate(codePoint);
Character.lowSurrogate(codePoint);
用于检索代理,给定代码点。
一旦有了这些,您就可以将它们写入 ByteBuffer
并获取相应的 byte[]
并使用它创建一个新的 String
> 采用 UTF-16 编码。
// byte[] bytes = new byte[] { (byte) 0xD8, 0x34, (byte) 0xDD, 0x1E };
String text = "𝄞"; // new String(bytes, StandardCharsets.UTF_16);
int codePoint = text.codePointAt(0);
ByteBuffer buf = ByteBuffer.allocate(4);
buf.putChar(Character.highSurrogate(codePoint));
buf.putChar(Character.lowSurrogate(codePoint));
byte[] data = buf.array();
// recreated
String rep = new String(data, StandardCharsets.UTF_16);
关于java - 描绘超出字符范围,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26023898/