如何在Java中获取unicode字符的十进制值?
问题描述:
我需要一种编程方式来获取String中每个字符的十进制值,以便我可以将它们编码为HTML实体,例如:
I need a programmatic way to get the decimal value of each character in a String, so that I can encode them as HTML entities, for example:
UTF-8:
著者名
十进制:
著者名
答
我怀疑你只是对从 char
到 int
,这是隐含的:
I suspect you're just interested in a conversion from char
to int
, which is implicit:
for (int i = 0; i < text.length(); i++)
{
char c = text.charAt(i);
int value = c;
System.out.println(value);
}
编辑:如果你想处理代理对,你可以使用类似的东西:
If you want to handle surrogate pairs, you can use something like:
for (int i = 0; i < text.length(); i++)
{
int codePoint = text.codePointAt(i);
// Skip over the second char in a surrogate pair
if (codePoint > 0xffff)
{
i++;
}
System.out.println(codePoint);
}