Erlang 和带有西里尔字母的二进制

2024-01-16

我需要能够使用其中包含西里尔字符的二进制文件。我尝试只写<<"абвгд">>但我收到了 badarg 错误。

如何在 Erlang 中使用西里尔字母(或 unicode)字符串?


如果你想输入上面的表达式erlang shell, 请阅读unicode模块用户手册。 功能character_to_binary, and character_to_list都是可逆函数。下面是一个例子:

([email protected] /cdn-cgi/l/email-protection)37> io:getopts().
[{expand_fun,#Fun<group.0.33302583>},
 {echo,true},
 {binary,false},
 {encoding,unicode}]

([email protected] /cdn-cgi/l/email-protection)40> A = unicode:characters_to_binary("上海").
<<228,184,138,230,181,183>>

([email protected] /cdn-cgi/l/email-protection)41> unicode:characters_to_list(A).
[19978,28023]

([email protected] /cdn-cgi/l/email-protection)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]])

([email protected] /cdn-cgi/l/email-protection)46> io:format("~ts~n",[ unicode:characters_to_list(A,utf8)]).
上海
ok

如果你想使用unicode:characters_to_binary("上海").直接看源码,稍微复杂一点。你可以先尝试一下,看看有什么不同。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Erlang 和带有西里尔字母的二进制 的相关文章

随机推荐