如果你想输入上面的表达式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("上海").
直接看源码,稍微复杂一点。你可以先尝试一下,看看有什么不同。