abhishek ranjan
abhishek ranjan

Reputation: 652

How to convert a little-endian UTF-16 unicode to a erlang string

I have a string value as aman which is printed as <<97,0,109,0,97,0,110,0>> and when I do:

A=<<97,0,109,0,97,0,110,0>>
erlang:binary_to_list(A)

I get [97,0,109,0,97,0,110,0]
But instead I need a string as "aman" or simply aman

How can this be done?

Upvotes: 3

Views: 540

Answers (1)

legoscia
legoscia

Reputation: 41528

Use unicode:characters_to_list/2, specifying the encoding of the binary in the second argument:

> A = <<97,0,109,0,97,0,110,0>>.
<<97,0,109,0,97,0,110,0>>
> unicode:characters_to_list(A, {utf16, little}).
"aman"

Upvotes: 6

Related Questions