Getting Unicode value for char: Java

I am looking for a way to get the Unicode value for a given char and, if possible, save it as an integer. Any built-in method for this in Java, or do I need to encode my own?

Context

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

I thought about using ASCII values ​​for char, specifying char as int, but then I read about Unicode on the Internet and realized my mistake.

Any help would be appreciated.

+4
source share
1 answer

The Java programming language presents text in sequences of 16-bit blocks of code using UTF-16 encoding.

Therefore, this is enough:

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

By JLS 3.10.4

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

+3
source

Source: https://habr.com/ru/post/1487529/


All Articles