tags:

views:

154

answers:

1

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

+8  A: 

Strings are lists of integers in erlang, and therefore you can use the list_to_binary BIF:

1> list_to_binary("hello world").
<<"hello world">>
tux21b
Thanks. My brain wasn't working and I kept doing "string_to_binary" in a desperate attempt to prove the documentation, google, and common sense wrong. :)
Zubair
I also enter `string_to_binary` quite often, but the compiler keeps remaining me that there isn't such a thing ;)
tux21b
You will also want to keep in mind what happens when the string contains non-ASCII characters. Some function from the `unicode` module might be more appropriate than list_to_binary for such occasions.
ndim