utf 8 - How do I turn a Unicode code point into a Unicode String? -


i have string representing unicode code point, "272d". how turn "✭"?

elixir understands unicode:

iex> << 10029 :: utf8 >> "✭"  iex> "x{272d}" "✭" 

but need function takes in 4 characters , returns unicode string:

def from_code_point(<< code_point :: size(32) >>)   ??? end 

or possibly

def from_code_point(<< a, b, c, d >>)   ??? end 

i tried macro:

defmacro from_code_point(<< code_point :: size(32) >>)   quote     "x{unquote(code_point)}"   end end 

but returns "x{unquote(code_point)}".

a unicode codepoint number, first thing need parse string see value represents. can use binary_to_integer/2 (available in r16, r15 you'd need go through binary_to_list/1 , list_to_integer/2.

once have numerical value of codepoint, can plonk in binary (which underlying representation of string) telling elixir number you're passing unicode codepoint, so

def to_string(input)   <<binary_to_integer(input, 16) :: utf8>> end 

if have extract out of larger string, can put string.slice/3 in between so

def to_string2(input)   codepoint = string.slice(input, 0, 4)   <<binary_to_integer(codepoint, 16) :: utf8>> end 

Comments

Popular posts from this blog

html5 - What is breaking my page when printing? -

c# - must be a non-abstract type with a public parameterless constructor in redis -

ajax - PHP/JSON Login script (Twitter style) not setting sessions -