Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Escape 5 or 6 hex digit Unicode codepoint

Tags:

elixir

In the Elixir documentation it says that you can use the following escapes:

  • \xNN - A byte represented by the hexadecimal NN
  • \uNNNN - A Unicode code point represented by NNNN

So what if I want to escape a codepoint that's longer than 4 hex digits, i.e. is outside the Basic Multilingual Plane, like 🚒 U+1f692 FIRE ENGINE or all the Private Use characters?


2 Answers

There is one syntax that is not described on that page:

"\u{1f692}"
like image 66
Amadan Avatar answered Sep 05 '25 14:09

Amadan


You can also use a binary: <<0x1f692::utf8>>

iex> cp = 0x1f692
iex> "Nee #{<<cp::utf8>>} naw"
"Nee 🚒 naw"

Or put it in a charlist [0x1f692]:

iex> "Nee #{[cp]} naw"
"Nee 🚒 naw"

Both of these are useful if you have the codepoint in a variable.

like image 26
Adam Millerchip Avatar answered Sep 05 '25 15:09

Adam Millerchip



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!