30

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

String = "Hello"
%% should be
Binary = <<"Hello">>
2240
  • 1,547
  • 2
  • 12
  • 30
yazz.com
  • 57,320
  • 66
  • 234
  • 385

2 Answers2

52

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">>
2240
  • 1,547
  • 2
  • 12
  • 30
tux21b
  • 90,183
  • 16
  • 117
  • 101
  • 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. :) – yazz.com Feb 15 '10 at 21:00
  • I also enter `string_to_binary` quite often, but the compiler keeps remaining me that there isn't such a thing ;) – tux21b Feb 15 '10 at 21:04
  • 3
    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 Feb 22 '10 at 15:54
  • 1
    It work only if string contain 1 byte char, else 81> list_to_binary("hello брбр"). ** exception error: bad argument in function list_to_binary/1 called as list_to_binary([104,101,108,108,111,32,1073,1088,1073,1088]) 82> –  Jul 08 '11 at 16:58
8

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

ombud
  • 199
  • 2
  • 10