检索字符的 Unicode 值:Java

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17241199/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-01 01:25:17  来源:igfitidea点击:

Retrieving the Unicode value of char: Java

javaunicode

提问by HasKal

I am looking for a way to retrieve the Unicode value for a given char, and if possible store it as an integer. Any inbuilt method for this in Java, or do I have to code my own?

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

Context

语境

I am building a basic encryption program for fun. What I need is to map each character in the Unicode set to an integer, which I can then manipulate in my encryption formula.

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

I thought about using ASCII values for char by typecasting the char as an int, but then I read about Unicode online, and realised my mistake.

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

Any help will be appreciated.

任何帮助将不胜感激。

回答by NINCOMPOOP

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

Java 编程语言使用 UTF-16 编码以 16 位代码单元的序列表示文本。

Hence this is enough :

因此,这就足够了:

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

As per JLS 3.10.4

根据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.

字符字面量只能表示 UTF-16 代码单元(第 3.1 节),即它们仅限于从 \u0000 到 \uffff 的值。补充字符必须表示为字符序列中的代理对或整数,具体取决于它们所使用的 API。