0xA*_*xAX 5 unicode binary erlang
我需要能够使用带有西里尔字符的二进制文件.我尝试写作<<"?????">>
但我得到了一个badarg错误.
如何在Erlang中使用Cyrillic(或unicode)字符串?
Che*_* Yu 12
如果您想输入上述表达式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
Run Code Online (Sandbox Code Playgroud)
如果要unicode:characters_to_binary("??").
直接在源代码中使用,则会更复杂一些.您可以先尝试一下,找出差异.
Erlang编译器会将代码解释为ISO-8859-1编码文本,这会将您限制为拉丁字符.虽然您可能会碰到一些可能在Unicode中具有相同字节表示的ISO字符,但这不是一个好主意.
您希望确保编辑器读取和写入ISO-8859-1,并且您希望尽可能避免使用文字.从文件中获取这些字符串.