>, but I had ...">

Erlang and binary with cyrillic

I need to be able to use binary files with Cyrillic characters in them. I tried just writing <<"">>, but I had a badarg error.

How can I work with Cyrillic (or unicode) strings in Erlang?

+5
source share
2 answers

If you want to enter the specified expression in erlang shell, read the user manual for the module unicode. Function character_to_binaryand character_to_listare simultaneously a reverse function. The following is an example:

(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

If you want to use unicode:characters_to_binary("上海").directly in the source code, this is a little more complicated. You can try to find the difference first.

+12
source

Erlang ISO-8859-1 , . ISO, , , Unicode, .

, ISO-8859-1, . .

+6

All Articles