In Dart/Flutter, given a Mandarin word "中", how to get the Unicode hex (4E2D) or decimal (20013) value?
I get the value from this website.
I can only convert the word into hex by doing HEX.encode(utf8.encode("中"));
, which is not the Unicode value I need. I am very confused, need some explanation here.
2
Answers
utf8.encode will give you the utf-8 byte values, but you are actually wanting the utf-16 code points.
This example shows the values for the utf8 bytes and utf16 code points.
And the output:
While this works for your specific character, if you are working with input that contains surrogate pairs you may want to look at string.runes instead of string.codeUnits
To get the decimal or unicode value from a Chinese word, try any of the following.
Alternatively, you could also do: