How to get the decimal value of Unicode characters in Java?
•
Java
I need a programming way to get the decimal value of each character in the string so that I can encode them as HTML entities, for example:
UTF-8:
著者名
decimal system:
著者名
Solution
I suspect that you are only interested in the conversion from char to int, which is implied:
for (int i = 0; i < text.length(); i++) { char c = text.charAt(i); int value = c; System.out.println(value); }
Edit: if you want to handle proxy pairs, you can use something similar:
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); }
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码