In Erlang how do I convert a String to a binary value?

the unicode (utf-8/16/32) character set needs more number of bits to express characters that are greater than 1-byte in length: this is why the above call failed for any byte value > 255 (the limit of information that a byte can hold, and which is sufficient for IS0-8859/ASCII/Latin1)

to correctly handle unicode characters you'd need to use

unicode:characters_to_binary() R1[(N>3)]

instead, which can handle both Latin1 AND unicode encoding.

HTH ...


In Erlang strings are represented as a list of integers. You can therefore use the list_to_binary (built-in-function, aka BIF). Here is an example I ran in the Erlang console (started with erl):

1> list_to_binary("hello world").
<<"hello world">>

Tags:

Erlang