如何在Java中获取unicode字符的十进制值? [英] How do I get the decimal value of a unicode character in Java?

查看:305
本文介绍了如何在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);
}

这篇关于如何在Java中获取unicode字符的十进制值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆