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

appshare.co picture appshare.co · Feb 15, 2010 · Viewed 18k times · Source

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

String = "Hello"
%% should be
Binary = <<"Hello">>

Answer

tux21b picture tux21b · Feb 15, 2010

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">>