[erlang-questions] characters_to_list vs characters_to_binary

Raimo Niskanen raimo+erlang-questions@REDACTED
Fri Oct 2 09:32:24 CEST 2009


On Fri, Oct 02, 2009 at 02:11:36PM +0900, Ngoc Dao wrote:
> I am using Erlang R13B02-1 with Japanese data.
> 
> The doc of unicode module says that their results are the same, only
> one in list and one in binary. But:
> 
> unicode:characters_to_list([230,151,165,230,156,172,232,170,158,231,137,136]).
> => [230,151,165,230,156,172,232,170,158,231,137,136]
> 
> unicode:characters_to_binary([230,151,165,230,156,172,232,170,158,231,137,136]).
> => <<195,166,194,151,194,165,195,166,194,156,194,172,195,168,
> 194,170,194,158,195,167,194,137,194,136>>
> 
> Is there something wrong?

I think you misinterpreted the documentation. The results are
the same i.e the same character sequence, but the binary
result from unicode:characters_to_binary/1 is encoded in utf8
while unicode:characters_to_list/1 simply returns a list
of unicode code points (character codes).
unicode:characters_to_list(<<195,166,194,151,194,165,195,166,
    194,156,194,172,195,168,194,170,194,158,195,167,194,137,194,136>>).
=> [230,151,165,230,156,172,232,170,158,231,137,136]

The basic principle: binaries are encoded,
lists just contain character codes
(and (encoded) binaries).

> 
> ________________________________________________________________
> erlang-questions mailing list. See http://www.erlang.org/faq.html
> erlang-questions (at) erlang.org

-- 

/ Raimo Niskanen, Erlang/OTP, Ericsson AB


More information about the erlang-questions mailing list