How To Convert An Elixir Binary To A String?

前端 未结 6 1553
天命终不由人
天命终不由人 2020-12-10 02:41

So I\'m trying to convert a binary to a string. This code:

t = [{<<71,0,69,0,84,0>>}]
String.from_char_list(t)

But I\'m gettin

6条回答
  •  自闭症患者
    2020-12-10 03:10

    In reference to http://erlang.org/pipermail/erlang-questions/2010-December/054885.html

    You can use :unicode.characters_to_list(binary_string, {:utf16, :little}) to verify result and store too

    IEX eval

    iex(1)> y                                                
    <<115, 0, 121, 0, 115, 0>>
    iex(2)> :unicode.characters_to_list(y, {:utf16, :little})
    'sys'
    

    Note : Value printed as sys for <<115, 0, 121, 0, 115, 0>>

提交回复
热议问题