Java Char以其unicode十六进制字符串表示形式,反之亦然 [英] Java Char to its unicode hexadecimal string representation and vice-versa

查看:310
本文介绍了Java Char以其unicode十六进制字符串表示形式,反之亦然的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将Java字符的十六进制代码生成为字符串,然后稍后再次解析这些字符串.我在此处发现可以按以下方式执行解析:

I need to generate the hexadecimal code of Java characters into strings, and parse those strings again later. I found here that parsing can be performed as following:

char c = "\u041f".toCharArray()[0];

我希望能找到更优雅的东西,例如

I was hoping for something more elegant like Integer.valueOf() for parsing.

如何正确生成十六进制unicode?

How about generating the hexadecimal unicode properly?

推荐答案

在进行了更深入的阅读之后,javadoc说

After doing some deeper reading, the javadoc says the Character methods based on char parameters do not support all unicode values, but those taking code points (i.e., int) do.

因此,我一直在进行以下测试:

Hence, I have been performing the following test:

    int codePointCopyright = Integer.parseInt("00A9", 16);

    System.out.println(Integer.toHexString(codePointCopyright));
    System.out.println(Character.isValidCodePoint(codePointCopyright));

    char[] toChars = Character.toChars(codePointCopyright);
    System.out.println(toChars);

    System.out.println();

    int codePointAsian = Integer.parseInt("20011", 16);

    System.out.println(Integer.toHexString(codePointAsian));
    System.out.println(Character.isValidCodePoint(codePointAsian));

    char[] toCharsAsian = Character.toChars(codePointAsian);
    System.out.println(toCharsAsian);

我得到了:

因此,我不应该在问题中谈论char,而是谈论字符数组,因为Unicode字符可以用多个char表示.另一方面,int涵盖了所有内容.

Therefore, I should not talk about char in my question, but rather about array of chars, since Unicode characters can be represented with more than one char. On the other side, an int covers it all.

这篇关于Java Char以其unicode十六进制字符串表示形式,反之亦然的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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