gpt4 book ai didi

java - 检索 char : Java 的 Unicode 值

转载 作者:搜寻专家 更新时间:2023-11-01 03:07:34 28 4
gpt4 key购买 nike

我正在寻找一种方法来检索给定字符的 Unicode 值,并在可能的情况下将其存储为整数。在 Java 中有任何内置方法吗,还是我必须自己编写代码?

上下文

为了好玩,我正在构建一个基本的加密程序。我需要的是将 Unicode 集中的每个字符映射到一个整数,然后我可以在我的加密公式中对其进行操作。

我想通过将 char 类型转换为 int 来使用 char 的 ASCII 值,但后来我在网上阅读了 Unicode,并意识到我的错误。

我们将不胜感激。

最佳答案

The Java programming language represents text in sequences of 16-bit code units, using the UTF-16 encoding.

因此这就足够了:

char character='a';
int code = character;
System.out.println(code);

根据 JLS 3.10.4

Character literals can only represent UTF-16 code units (§3.1), i.e., they are limited to values from \u0000 to \uffff. Supplementary characters must be represented either as a surrogate pair within a char sequence, or as an integer, depending on the API they are used with.

关于java - 检索 char : Java 的 Unicode 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17241199/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com