Reputation: 4094
I want to iterate over the set of Chinese characters in Elixir given by Unicode. I read the documentation and it says I can use the '?' operator to get the codepoint as an integer and then can increment it. Now I just need to do the inverse, from codepoint to integer. Is there an easy way to do that? I did not find any. For instance, in Python you would do
>>> chr(ord("一") + 1)
'丁'
Upvotes: 12
Views: 6244
Reputation: 222060
There is no character data type in Elixir, but to convert a codepoint to a string containing that character (encoded as UTF-8), you can use either <<x::utf8>>
or List.to_string([x])
:
iex(1)> x = ?一 + 1
19969
iex(2)> <<x::utf8>>
"丁"
iex(3)> List.to_string([x])
"丁"
Upvotes: 16