[erlang-questions] strings vs binaries

Loïc Hoguin essen@REDACTED
Wed Aug 19 13:44:23 CEST 2015


On 08/19/2015 12:04 PM, zxq9 wrote:
> Going out again, of course, iolists can accept binaries and strings

That's actually incorrect. iolist() does not include string() in the 
types it allows. The type is:

iolist() :: maybe_improper_list(byte() | binary() | iolist(), binary() | [])

If you have a string() that only has 0..255 characters, then it will 
work; but this is incidental. A Unicode string inside an iolist() will 
not work.

So if you convert a binary to Unicode string to do manipulation, you 
have to *convert it back*.

The fact that strings are faster is not very interesting considering the 
amount of conversion you have to do and the extra memory you end up 
using. That, and you can just send your binary to the i18n library and 
let it handle things anyway:

i18n_string:from_utf8(Bin)

That's assuming you use the i18n library, but if you're going to do 
anything with Unicode that's pretty much the only good choice.

Cheers,

-- 
Loïc Hoguin
http://ninenines.eu
Author of The Erlanger Playbook,
A book about software development using Erlang



More information about the erlang-questions mailing list