Ignas
Ignas

Reputation: 379

Erlang Terms to Unicode String

I have a list of tuples, produced by some function, which looks like:

[{"a","ą"}, {"ą","a"}, {"a","o"}, {"o","e"}]

But when I print it, I see in terminal:

[{"a",[261]}, {[261],"a"}, {"a","o"}, {"o","e"}]

I usually print it with this command:

io:format("~p~n", [functionThatGeneratesListOfTuples()]),

So far I found that you need to use ~ts when printing Unicode strings, so I tried this:

Pairs = functionThatGeneratesListOfTuples(), PairsStr = io_lib:format("~p", [Pairs]), io:format("~ts~n", [PairsStr]),

Is there any possibility to achieve that Unicode Strings would be represented appropriately?

Upvotes: 4

Views: 263

Answers (1)

RichardC
RichardC

Reputation: 10557

The heuristics for detecting lists-of-integers as strings only recognize Latin-1 characters by default, so [65,66,67] is printed as "ABC" but [665,666,667] is printed as "[665,666,667]" even if you use ~tp. You have to start Erlang as erl +pc unicode to make it accept printable unicode code points above 255. In that mode, [665,666,667] is printed as "ʙʚʛ" with ~tp (but not with ~p).

See http://erlang.org/doc/man/io.html#printable_range-0 for more info, and also this recent improvement of the documentation, which will be included in OTP 21: https://github.com/erlang/otp/pull/1737/files

Upvotes: 5

Related Questions