我需要能够使用带有西里尔字符的二进制文件。我试着写<<"абвгд">>
但我收到了一个 badarg 错误。
如何在 Erlang 中使用西里尔文(或 unicode)字符串?
最佳答案
如果要在 erlang shell
中输入上述表达式, 请阅读 unicode
模块用户手册。
功能 character_to_binary
, 和 character_to_list
都是可逆的功能。下面是一个例子:
(emacs@yus-iMac.local)37> io:getopts().
[{expand_fun,#Fun<group.0.33302583>},
{echo,true},
{binary,false},
{encoding,unicode}]
(emacs@yus-iMac.local)40> A = unicode:characters_to_binary("上海").
<<228,184,138,230,181,183>>
(emacs@yus-iMac.local)41> unicode:characters_to_list(A).
[19978,28023]
(emacs@yus-iMac.local)45> io:format("~s~n",[ unicode:characters_to_list(A,utf8)]).
** exception error: bad argument
in function io:format/3
called as io:format(<0.30.0>,"~s~n",[[19978,28023]])
(emacs@yus-iMac.local)46> io:format("~ts~n",[ unicode:characters_to_list(A,utf8)]).
上海
ok
如果你想使用
unicode:characters_to_binary("上海").
直接在源码中,稍微复杂一点。你可以先试试看有什么不同。
关于unicode - Erlang 和带有 Cyrillic 的二进制文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10596193/