【发布时间】:2015-08-17 00:43:13
【问题描述】:
例如,对于 IoDevices,可以使用 io:getopts/1,但我找不到任何用于纯字符串的方法。
例如,
ManPage = os:cmd("man ls").
% [76,83,40,49,41,32,32,32,32,32,32,32,32,32,32,32,32,32,32,
% 32,32,32,32,32,32,32,32,32,32|...]
io:format("~p~n",[ManPage]).
% [76,83,40,49,41,(...)
io:format("~ts~n",[ManPage]).
% LS(1) User Commands LS(1)
% NAME
% ls - list directory contents
文档on using Unicode in Erlang 仅提及启发式方法,但它可能已过时,因为根据示例io_lib:format/2 和~ts 控制字符会产生utf-8 输出。用 Erlang 18.0 试试:
Bullet = "\x{2022}".
% [8226]
io:format("~ts~n", [Bullet]).
% •
% ok
io:format("~ts~n", ["•"]).
% •
% ok
io_lib:format("~ts~n", [Bullet]).
% [[8226],"\n"]
我知道我可以使用 unicode:characters_to_binary/{1,2,3},因为它接受 latin1 或 utf8 编码的输入并输出 unicode 编码的输出,但我很好奇是否还有其他方法。
有趣的是,unicode:characters_to_binary/1 工作正常,而 unicode:characters_to_list/1 不能(或者我在滥用它)。
unicode:characters_to_binary(ManPage).
% <<"LS(1) User Commands LS(1)\n\n\n\nNAME\n "...>>
unicode:characters_to_list(ManPage).
% [76,83,40|...]
unicode:characters_to_list(ManPage, latin1).
% {error,"LS(1) User Commands LS(1",
[8208,10,32|...]}
【问题讨论】:
标签: unicode encoding utf-8 erlang