Reputation: 58766
In Erlang how do I convert a string
to a binary
value?
String = "Hello"
%% should be
Binary = <<"Hello">>
Upvotes: 32
Views: 21985
Reputation: 94639
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">>
Upvotes: 53
Reputation: 197
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 ...
Upvotes: 8