• erlang 中 binary 的一些操作


    预:

    7> [I || <<I:1>> <= <<"abc">>].
    [0,1,1,0,0,0,0,1,0,1,1,0,0,0,1,0,0,1,1,0,0,0,1,1]
    8> [I || <<I:8>> <= <<"abc">>].
    "abc"

    (0)list_to_binary("www.Shank.kuang").
    <<"www.Shank.kuang">>
    binary:list_to_bin("www.Shank.kuang").
    <<"www.Shank.kuang">>

    byte_size(<<"www.Shank.me">>).
    12
    (1)返回一个二进制数据里指定位置(从 0 开始)的数据(整数的形式),
    如果 Pos >= byte_size(Subject),则会发生一个 badarg 的异常错误。
    binary:at(<<"a", 1, "b", 2, "c", 3, "d", 4>>, 2).
    98
    binary:at(<<"a", 1, "b", 2, "c", 3, "d", 4>>, 3).
    2
    (2)binary:bin_to_list(<<"erlang">>).
    "erlang"
    % 从0开始
    binary:bin_to_list(<<"erlang">>, {1 ,3}).
    "rla"
    binary:bin_to_list(<<"erlang">>, {1 ,33}).
    ** exception error: bad argument
    in function binary:bin_to_list/2
    called as binary:bin_to_list(<<"erlang">>,{1,33})
    binary:bin_to_list(<<"erlang">>, 1, 3).
    "rla"
    (3)Binary = <<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>>,
    binary:copy(Binary).
    <<1,2,3,4,5,6,7,8,9,10>>
    (4)binary:copy(<<"www.Shank.kuang">>, 4).
    <<"www.Shank.kuangwww.Shank.kuangwww.Shank.kuangwww.Shank.kuang">>
    (5)返回一个二进制 Subject 的第一个字节的 ASCII 码。
    如果二进制 Subject 的长度为 0,那么会有一个异常抛出
    binary:first(<<"12345">>).
    49
    binary:last(<<"12345">>).
    53

    (6)Converts the binary digit representation, in big or little endian, of a positive integer in Subject to an Erlang integer().
    注:representation of 算法表示;
    默认是big端
    binary:decode_unsigned(<<169,138,199>>).
    11111111
    binary:decode_unsigned(<<169,138,199>>,big).
    11111111
    binary:encode_unsigned(11111111,big).
    <<169,138,199>>

    (7)返回在列表二进制数据里最长的公共后缀(前缀)长度
    binary:longest_common_suffix([<<"erlang">>, <<"fang">>]).
    3
    binary:longest_common_suffix([<<"erlang">>, <<"perl">>]).
    0
    binary:match(<<"abcde">>, [<<"bcde">>, <<"cd">>]).
    {1,4}
    binary:longest_common_prefix([<<"erlang">>,<<"ergonomy">>]).
    2
    binary:longest_common_prefix([<<"erlang">>,<<"perl">>]).
    0

    (8)这个binary:referenced_byte_size 是用于获取binary 数据的来源binary的大小:
    eg:
    1> A = binary:copy(<<1>>,100).
    <<1,1,1,1,1 ...
    2> byte_size(A).
    100
    3> binary:referenced_byte_size(A)
    100
    4> <<:10/binary,B:10/binary,/binary>> = A.
    <<1,1,1,1,1 ...
    5> byte_size(B).
    10
    6> binary:referenced_byte_size(B)
    100
    eg:2
    113> Binary = <<1, 2, 3, 4, 5, 6, 7, 8, 9, 10>>,
    113> <<_:4/binary, Bin:4/binary, _/binary>> = Binary,
    113> binary:referenced_byte_size(Bin).
    10
    (9)
    114> Bin = <<"www.dhq.me">>,
    114> binary:split(Bin, <<".">>).
    ** exception error: no match of right hand side value <<"www.dhq.me">>
    115> binary:split(<<"www.dhq.me">>, <<".">>).
    [<<"www">>,<<"dhq.me">>]
    116> binary:split(<<"www.kuang.feng.me.aab.">>, <<".">>).
    [<<"www">>,<<"kuang.feng.me.aab.">>]
    117> binary:split(<<1,255,4,0,0,0,2,3>>, [<<0,0,0>>,<<2>>]).
    [<<1,255,4>>,<<2,3>>]
    118>
    1> binary:split(<<"banana">>,[<<"a">>],[{scope,{2,3}}]).
    [<<"ban">>,<<"na">>]
    2> binary:split(binary:part(<<"banana">>,{2,3}),[<<"a">>],[]).
    [<<"n">>,<<"n">>]
    (10)
    Searches for the first occurrence of Pattern in Subject and returns
    the position and length.
    The function will return {Pos, Length} for the binary in Pattern
    starting at the lowest position in Subject, Example:
    binary:match(<<"abcdefghidg">>, [<<"bc">>,<<"cd">>],[]).
    {1,2}
    130> binary:match(<<"abcdefghidg">>, [<<"cd">>,<<"bc">>],[]).
    {1,2}

    有匹配了, 添加一个mnesia匹配的例子:

    For example to find the names of all male persons with an age over 30 in table Tab do:
    
    MatchHead = #person{name='$1', sex=male, age='$2', _='_'},
    Guard = {'>', '$2', 30},
    Result = '$1',
    mnesia:select(Tab,[{MatchHead, [Guard], [Result]}]),
    
    

    (11)
    Constructs a new binary by replacing the parts in Subject matching Pattern with the content of Replacement.

    If the matching sub-part of Subject giving raise to the replacement is to be inserted in the result, the option {insert_replaced, InsPos} will insert the matching part into Replacement at the given position (or positions) before actually inserting Replacement into the Subject. Example:

    insert_replaced 属性的意思就是说:不直接替换了,把匹配到的
    string按照 一个或者若干个位置插入到 用于替换的字符串的位置像
    eg2是插入到 字符串"[]"的第一个位置,
    eg4就是把匹配到的 "b"插入到字符串"[-]"的第一和第二个位置前面

    1> binary:replace(<<"abcde">>,<<"b">>,<<"[]">>,[{insert_replaced,1}]).
    <<"a[b]cde">>
    2> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[]">>,
    [global,{insert_replaced,1}]).
    <<"a[b]c[d]e">>
    3> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[]">>,
    [global,{insert_replaced,[1,1]}]).
    <<"a[bb]c[dd]e">>
    4> binary:replace(<<"abcde">>,[<<"b">>,<<"d">>],<<"[-]">>,
    [global,{insert_replaced,[1,2]}]).
    <<"a[b-b]c[d-d]e">>
    1> Bin = <<1,2,3,4,5,6,7,8,9,10>>.
    2> binary:part(Bin,{byte_size(Bin), -5}).
    <<6,7,8,9,10>>

    参考:
    http://dhq.me/erlample/
    http://www.erlang.org/doc/man/binary.html

  • 相关阅读:
    洛谷P4206 [NOI2005]聪聪与可可(期望dp+最短路)
    bzoj2064: 分裂(状压dp)
    Centos7搭建ansible运维自动化工具
    Centos7自动式脚本搭建jumpserver
    TSP问题—Hopfield神经网络算法
    TSP问题—近似算法
    狭义相对论的数学推导
    线性规划实战—投资的收益和风险
    线性规划的算法分析
    一阶非齐次线性微分方程的算法
  • 原文地址:https://www.cnblogs.com/ShankYan/p/4364848.html
Copyright © 2020-2023  润新知