zoukankan      html  css  js  c++  java
  • erlang中文问题

    借鉴:https://www.cnblogs.com/me-sa/archive/2012/05/31/erlang-unicode.html

    1、中文list_to_binary报错

    使用unicode:characters_to_binary(L2).即可

    ep:

    Eshell V5.9 (abort with ^G)
    1> list_to_binary("中国").
    ** exception error: bad argument
    in function list_to_binary/1
    called as list_to_binary([20013,22269])
    2>
    2>
    2> unicode:characters_to_binary("中国").
    <<228,184,173,229,155,189>>
    3>

    2、如何判断字符串为中文字符串

    1)erlang shell下

    re:run("hello 中国 ren", "[x{4e00}-x{9fff}]+", [unicode]).

    ep:

    4> re:run("hello 中国 ren", "[x{4e00}-x{9fff}]+", [unicode]).
    {match,[{6,6}]}
    5> re:run("hello ren", "[x{4e00}-x{9fff}]+", [unicode]).
    nomatch
    6>

    3、代码模块中

    re:run("中国", "([x81-xfe][x40-xfe])+", [unicode])

    ep:

    -module(test).
    -export([test/0]).


    test()->
    R = re:run("中国", "([x81-xfe][x40-xfe])+", [unicode]),
    R1 = re:run("zhong guo", "([x81-xfe][x40-xfe])+", [unicode]),
    io:format("~p,~p~n",[R,R1]).

    Eshell V5.9 (abort with ^G)
    1> test:test().
    {match,[{0,12},{8,4}]},nomatch
    ok
    2>

    shell 与模块中不一致的原因:

    在模块文件进行编译的时候使用的是ISO-latin-1,其中的中文并不在其字符集中,所以转成了两组数字!被转成两组数字之后,也就无法被正则表达式命中了.而在Erlang Shell中,中文字符可以被正确编码,所以会被正则命中.而仔细关注一下正则表达式,其实就是大致上覆盖了中文字符在unicode字符集中对应的数值区间.
    对于这种情况只要让unicode避开编译阶段就可以了,比如把这类文本放在外部文本中.

    在 unicode 字符集环境下,中文会被编译为 [20013,22269,20154] 这样的双字节数字,
    在 latin1 字符集环境下,中文会被编译为 [228,184,173,229,155,189,228,186,186] 这样的单字节数字

    事实证明与文件编码没有关系, erlang编译器编译的时候使用latin字符集及编码格式, 中文会编译成单字节数字

    ISO Latin-1字符集是Unicode字符集的一个子集,对应于IE4+中Unicode字符指令表的前256个条目。其中Unicode字符为双字节16位,可以表示任何一种语言符号;而Latin-1字符集是单字节8位,只能够表示英文和西欧字符



  • 相关阅读:
    (网络流)ACM Computer Factory --POJ --3436
    (小数化分数)小数化分数2 -- HDU --1717
    (小数化分数)小数化分数2 --HDU --1717
    (网络流 模板 Dinic) Drainage Ditches --POJ --1273
    (网络流 模板 Edmonds-Karp)Drainage Ditches --POJ --1273
    (匹配)Oil Skimming -- hdu --4185
    (匹配 二维建图) Antenna Placement --POJ --3020
    (匹配)Antenna Placement --POJ --3020
    将截断字符串或二进制数据【转】
    C#中Abstract和Virtual 【转】
  • 原文地址:https://www.cnblogs.com/yanwei-wang/p/8308484.html
Copyright © 2011-2022 走看看