unicode - Erlang 和带有 Cyrillic 的二进制文件

标签 unicode binary erlang

我需要能够使用带有西里尔字符的二进制文件。我试着写<<"абвгд">>但我收到了一个 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/

相关文章:

Python3 将字符串转换为字节

.net - 在 .NET 中将非 ascii(unicode)数字字符串解析为整数

python - 从一系列数字生成二进制数据数组

erlang - Scalaris 与 CouchDB

ISO 8859-1 或 Unicode 中的 JavaScript 变量名称

python - CSV、Python : Using DictWriter correctly (ValueError: dict contains fields not in fieldnames)

character-encoding - mac 上的文件编码,charset=binary

c++ - 为什么这段代码总是返回零文件大小?

erlang - 如何在 Elixir 中将数字转换回字符串?

Erlang 热重载和 badfuns