erlang 中文编码显示乱码问题
许久没做erlang开发了,近期有网友问到erlang的问题。就抽时间看下。问题是这种。模块有中文。将中文直接打印出来。shell下显示会出现乱码。但假设先将中文转成binary。就行正常显示出来。
shell中文乱码问题
这里以一个简单的样例,说明下:
-module(m).
-compile(export_all). test() ->
io:format("~ts~n", ["中国"]),
io:format("~ts~n", [list_to_binary("中国")]).
以R17之前的erlang版本号编译。然后測试下结果:
Eshell V5.10.3 (abort with ^G)
1> c(m).
{ok, m}
2> m:test().
ä¸å½
中国
ok
{function, test, 0, 2}.
{label,1}.
{line,[{location,"erl.erl",4}]}.
{func_info,{atom,erl},{atom,test},0}.
{label,2}.
{allocate,0,0}.
{move,{literal,[[228,184,173,229,155,189]]},{x,1}}.
{move,{literal,"~ts~n"},{x,0}}.
{line,[{location,"erl.erl",5}]}.
{call_ext,2,{extfunc,io,format,2}}.
{move,{literal,[<<228,184,173,229,155,189>>]},{x,1}}.
{move,{literal,"~ts~n"},{x,0}}.
{line,[{location,"erl.erl",6}]}.
{call_ext_last,2,{extfunc,io,format,2},0}.
实际上,erlang在编译代码时会做优化。数据已知的话。list_to_binary在编译期就被优化掉了。
test() ->
io:format("~ts~n", [[228,184,173,229,155,189]]),
io:format("~ts~n", [<<228,184,173,229,155,189>>]).
io:format/2 对中文的处理
看了 io:format/2 的实现代码,关键代码为下面两步:
3> L = io_lib:format("~ts",[<<228,184,173,229,155,189>>]).
[[20013,22269]]
4> io:put_chars(L).
中国ok
%% io_lib.erl format(Format, Args) ->
case catch io_lib_format:fwrite(Format, Args) of
{'EXIT',_} ->
erlang:error(badarg, [Format, Args]);
Other ->
Other
end.
实现代码在 io_lib_format模块,例如以下:
%% io_lib_format.erl fwrite(Format, Args) when is_atom(Format) ->
fwrite(atom_to_list(Format), Args);
fwrite(Format, Args) when is_binary(Format) ->
fwrite(binary_to_list(Format), Args);
fwrite(Format, Args) ->
Cs = collect(Format, Args), %% 收集格式化信息。生成控制结构
Pc = pcount(Cs), %% 计算请求打印的数量
build(Cs, Pc, 0). %% 解析控制结构,生成数据 collect([$~|Fmt0], Args0) -> %% 格式化參数以 ~打头,否则忽略
{C,Fmt1,Args1} = collect_cseq(Fmt0, Args0),
[C|collect(Fmt1, Args1)];
collect([C|Fmt], Args) ->
[C|collect(Fmt, Args)];
collect([], []) -> []. collect_cseq(Fmt0, Args0) ->
{F,Ad,Fmt1,Args1} = field_width(Fmt0, Args0),
{P,Fmt2,Args2} = precision(Fmt1, Args1),
{Pad,Fmt3,Args3} = pad_char(Fmt2, Args2),
{Encoding,Fmt4,Args4} = encoding(Fmt3, Args3),
{Strings,Fmt5,Args5} = strings(Fmt4, Args4),
{C,As,Fmt6,Args6} = collect_cc(Fmt5, Args5),
{{C,As,F,Ad,P,Pad,Encoding,Strings},Fmt6,Args6}. %% 检查format 參数含有 t, 然后打标记 unicode。其它记latin1
encoding([$t|Fmt],Args) ->
true = hd(Fmt) =/= $l, %% 确保不是传入 ~tl
{unicode,Fmt,Args};
encoding(Fmt,Args) ->
{latin1,Fmt,Args}.
再看下以上build部分的代码。代码过长,做了删节:
%% io_lib_format.erl
build([{C,As,F,Ad,P,Pad,Enc,Str}|Cs], Pc0, I) ->
S = control(C, As, F, Ad, P, Pad, Enc, Str, I), %% 处理控制结构
Pc1 = decr_pc(C, Pc0),
if
Pc1 > 0 -> [S|build(Cs, Pc1, indentation(S, I))];
true -> [S|build(Cs, Pc1, I)]
end;
build([$\n|Cs], Pc, _I) -> [$\n|build(Cs, Pc, 0)];
build([$\t|Cs], Pc, I) -> [$\t|build(Cs, Pc, ((I + 8) div 8) * 8)];
build([C|Cs], Pc, I) -> [C|build(Cs, Pc, I+1)];
build([], _Pc, _I) -> [].
control($w, [A], F, Adj, P, Pad, _Enc, _Str, _I) ->
term(io_lib:write(A, -1), F, Adj, P, Pad);
control($p, [A], F, Adj, P, Pad, Enc, Str, I) ->
print(A, -1, F, Adj, P, Pad, Enc, Str, I);
control($W, [A,Depth], F, Adj, P, Pad, _Enc, _Str, _I) when is_integer(Depth) ->
term(io_lib:write(A, Depth), F, Adj, P, Pad);
control($P, [A,Depth], F, Adj, P, Pad, Enc, Str, I) when is_integer(Depth) ->
print(A, Depth, F, Adj, P, Pad, Enc, Str, I);
control($s, [A], F, Adj, P, Pad, _Enc, _Str, _I) when is_atom(A) ->
string(atom_to_list(A), F, Adj, P, Pad);
control($s, [L0], F, Adj, P, Pad, latin1, _Str, _I) -> %% 处理 ~s,假设数据标记是 latin1
L = iolist_to_chars(L0),
string(L, F, Adj, P, Pad);
control($s, [L0], F, Adj, P, Pad, unicode, _Str, _I) -> %% 处理 ~s,假设数据标记是 unicode
L = cdata_to_chars(L0),
uniconv(string(L, F, Adj, P, Pad));
control($e, [A], F, Adj, P, Pad, _Enc, _Str, _I) when is_float(A) ->
%% 该函数太长了,不是讨论重点,做了删节
cdata_to_chars([C|Cs]) when is_integer(C), C >= $\000 ->
[C | cdata_to_chars(Cs)];
cdata_to_chars([I|Cs]) ->
[cdata_to_chars(I) | cdata_to_chars(Cs)];
cdata_to_chars([]) ->
[];
cdata_to_chars(B) when is_binary(B) -> %% 假设数据是binary,做一下unicode转换
case catch unicode:characters_to_list(B) of
L when is_list(L) -> L;
_ -> binary_to_list(B)
end.
可想而知。假设没有不是 ~ts。或者不是binary。都不会做转换。
探讨乱码问题
可是,对于拓展字符集每一个语种都有自己的定义方式,相同一段字符数据用不同的字符集就有不同的解释。
这就是乱码出现的原因。
假设原文以utf8记录。显示的时候又以utf8表示,就能正常显示。
正是这样。io_lib:format/2 也对编码做了特殊处理,但也局限于前面所述的情况。
直到R17后,erlang才将utf8做为源码的默认编码,在这之前。源码都以latin1形式读取和编译的。R17假设想改变默认编码,方法就是在模块首行加 %% coding: latin-1
utf8_list_to_string(List) ->
unicode:characters_to_list(list_to_binary(List)).
那读写utf8文件呢,怎么避免出现乱码呢?
read_and_write() ->
{ok,Bin} = file:read_file("file.txt"),
MyList = case unicode:characters_to_list(Bin) of
L when is_list(L) -> L;
_ -> binary_to_list(Bin)
end,
{ok,G} = file:open("new_file.txt",[write,{encoding,utf8}]),
io:put_chars(G,MyList),
file:close(G).
好久没搞erlang了。突然心血来潮,写了这篇文章。希望喜欢。
參考:
erlang 中文编码显示乱码问题的更多相关文章
- 【原创】python中文编码问题深入分析(二):print打印中文异常及显示乱码问题分析与解决
在学习python以及在使用python进行项目开发的过程中,经常会使用print语句打印一些调试信息,这些调试信息中往往会包含中文,如果你使用python版本是python2.7,或许你也会遇到和我 ...
- pycharm的console显示乱码和中文的配置
第一种方式: 在python脚本开始的地方加入指定编码方式 # -*- coding : UTF-8 -*- 第二种方式: 有些时候我们会得到这种格式的字符串: "name": & ...
- 关于PHP页面显示乱码问题的解决
关于PHP页面显示乱码问题的解决 网页乱码一直是网络编程高手都头痛的问题,我是一个PHP Web编程的初学者,学习当中也遇到了这个问题,查找了相关的资源,总结如下: 一般的中文编码:gb2312,gb ...
- Linux中文显示乱码?如何设置centos显示中文
Linux中文显示乱码?如何设置centos显示中文 怎么设置Linux系统中文语言,这是很多小伙伴在开始使用Linux的时候,都会遇到一个问题,就是终端输入命令回显的时候中文显示乱码.出现这个情况一 ...
- linux终端 字符界面 显示乱码
方法一:配置SSH工具 SecureCRT中文版配置 [全局选项]→[默认会话]→[编辑默认设置]→[终端]→[外观]→[字体]→[新宋体 10pt CHINESE_GB2312]→[字符编码 UTF ...
- 下载apk文件浏览器会直接打开并显示乱码的问题
今天同事反映他的apk文件在自己的老项目中下载有问题:下载apk文件浏览器会直接打开并显示乱码,在别的项目中就没有问题. 后分析response的content-type发现,老项目的类型是text/ ...
- Xshell个性化设置,解决Xshell遇到中文显示乱码的问题
在同事的推荐下,今天开始使用Xshell连接Linux,但是发现一个“遇到中文显示乱码”的问题, 同事的解决方案如下: 平常给Linux上传文件之前,先把文件转换成UTF-8编码形式, 然后设置Xsh ...
- (转)sqlplus中文显示乱码的问题
sqlplus中文显示乱码的问题 2010-07-19 11:33:26 分类: LINUX 在windows下sqlplus完全正常,可是到linux下,sqlplus中文显示就出问题了,总是显示“ ...
- mysql 乱码问题(程序界面显示正常,mysql command line显示乱码)
今天用java写一个程序,用的是mysql数据库.界面出现乱码,然后写了一个过滤器结果了乱码问题. 但是,当我在mysql command line 中查询数据的时候,在界面上显示正常的数据,在mys ...
随机推荐
- 浏览器在一次 HTTP 请求中,需要传输一个 4097 字节的文本数据给服务端,可以采用那些方式?
浏览器在一次 HTTP 请求中,需要传输一个 4097 字节的文本数据给服务端,可以采用那些方式? 存入 IndexdDB 写入 COOKIE 放在 URL 参数 写入 Session 使用 POST ...
- 动态修改字节码以替换用反射调用get set方法的形式
1. 起因 在前两天,为了解决websphere和JDK8上部署的应用发起webservice调用(框架用的cxf)时报错的问题,跟了一些代码,最终发现可以通过加上参数-Dcom.sun.xml.bi ...
- ASP.NET(一):Reques对象和Response对象的区别,以及IsPostBack属性的用法
导读:在ASP.NET的学习中,初步认识了其6大对象(严格说来只能算是属性):Request,Response,Application,Session,Server,OjectContext.这些对象 ...
- 九度oj 题目1031:xxx定律 题目1033:继续xxx定律
题目描述: 对于一个数n,如果是偶数,就把n砍掉一半:如果是奇数,把n变成 3*n+ 1后砍掉一半,直到该数变为1为止. 请计算需要经过几步才能将n变到1,具体可见样例. 输入: ...
- 九度oj 题目1021:统计字符
题目描述: 统计一个给定字符串中指定的字符出现的次数. 输入: 测试输入包含若干测试用例,每个测试用例包含2行,第1行为一个长度不超过5的字符串,第2行为一个长度不超过80的字符串.注 ...
- OMS数据库调整1
一.增加2T存储空间 1. 对磁盘进行分区并格式化 [root@oms-db01 ~]# fdisk -l Disk /dev/sda: 322.1 GB, 322122547200 bytes 2 ...
- Python之注册表增删改查(干货)
在Windows平台下,对注册表的增删改查的需求比较多,微软提供了很多用于访问,修改注册表等的API,我们可以使用诸如bat,或者C++等各种方式去访问修改注册表.无所不能的python下如何完成这些 ...
- BZOJ 1008: [HNOI2008]越狱【组合】
很少有的思路秒解.题意可以描述成对长度为n的格子有m种染色方案,问存在相邻两个格子同色的方案数,正难则反易,考虑问题的背面任意两个相邻的格子都不同色,第一个格子可以涂任意一种颜色m种可能,剩下的n-1 ...
- 【(最小权点基)tarjan强连通分量缩点+tarjan模板】HDU 5934 Bomb
[AC] #include<bits/stdc++.h> using namespace std; typedef long long ll; int n; ; ; const int i ...
- jmeter监控linux cpu 内存 网络 IO
下载地址:http://jmeter-plugins.org/downloads/all/ PerfMon: 用来监控Server的CPU.I/O.Memory等情况 ServerAgent-2.2. ...