【问题标题】:Do any other languages support bitstrings other than Erlang?除了 Erlang,还有其他语言支持位串吗?
【发布时间】:2014-07-20 15:46:04
【问题描述】:

我经常处理使用某种类型的 COMMAND|LENGTH|PARAMETERS 结构交换信息的“二进制”协议,其中 PARAMETERS 是任意数量的 TAG|LENGTH|VALUE 元组。 Erlang 通过模式匹配来提取消息中的值,例如:

M = <<1, 4, 1, 2, 16#abcd:16>>. <<1,4,1,2,171,205>>

使用 M 位串(遵循 COMMAND|LENGTH|PARAMETERS 格式的消息),我可以利用 Erlang 模式匹配来提取命令、长度、参数:

<<Command:8,Length:8,Parameters/binary>> = M. <<1,4,1,2,171,205>> Parameters. <<1,2,171,205>>

对于管理面向“位半字节”的协议,这是非常宝贵的!

是否有任何其他语言接近支持这样的语法,即使是通过附加库?

【问题讨论】:

    标签: erlang bit bitstring


    【解决方案1】:

    类似https://pypi.python.org/pypi/bitstring/3.1.3 for python 的东西可以让你在同一级别上做很多工作。

    从你的例子:

    from bitstring import BitStream
    M = BitStream('0x01040102abcd')
    [Command, Length, Parameters] = M.readlist('hex:8, hex:8, bits')
    

    Parameters 设为BitStream('0x0102abcd')

    【讨论】:

    • 看起来很有希望,关键是模式匹配提取;看了一眼链接,不清楚如何将位字符串映射到变量,但我会尝试一下。
    • 答案将被接受,我对其进行了编辑以提供一个反映 Erlang 的 BitStream 示例。
    【解决方案2】:

    OCaml 通过bitstringcamlp4 扩展名:https://code.google.com/p/bitstring/

      let bits = Bitstring.bitstring_of_file "image.gif" in
      bitmatch bits with
      | { ("GIF87a"|"GIF89a") : 6*8 : string; (* GIF magic. *)
          width : 16 : littleendian;
          height : 16 : littleendian } ->
          printf "%s: GIF image is %d x %d pixels" filename width height
      | { _ } ->
          eprintf "%s: Not a GIF image\n" filename
    

    另一个不太面向位但允许映射 C 值的 OCaml 库是 cstruct:https://github.com/mirage/ocaml-cstruct

    这让您可以编写内联代码,例如:

    cstruct pcap_header {
      uint32_t magic_number;   (* magic number *)
      uint16_t version_major;  (* major version number *)
      uint16_t version_minor;  (* minor version number *)
      uint32_t thiszone;       (* GMT to local correction *)
      uint32_t sigfigs;        (* accuracy of timestamps *)
      uint32_t snaplen;        (* max length of captured packets, in octets *)
      uint32_t network         (* data link type *)
    } as little_endian
    

    (有关更多信息,请参阅两个项目的 README)

    【讨论】:

    • 谢谢!另一种需要研究的语言,我以前没有使用过 OCaml!
    【解决方案3】:

    C 当然有一个相对未知的位域特性。 http://en.m.wikipedia.org/wiki/Bit_field

    【讨论】:

      【解决方案4】:

      除了已经提到的(Ocaml、Erlang、Python、C)之外,我至少数过 Racket [1]、SmallTalk / Squeak [2]、JavaScript [3]、Elixr [4](我意识到这是在Erlang BEAM VM,但我没有看到它提到)和 Haskell [5]

      Gustafsson 和 Sagonas [6] 的论文“Erlang 中 BitStream 编程的应用、实现和性能评估”有一个简短的(3 页)部分,标题为“实现”,其中涵盖了实现该系统的详细信息以及有效的抽象,因此您可以毫不费力地将其引入新语言。

      1:https://docs.racket-lang.org/bitsyntax/index.html

      2:https://eighty-twenty.org/2020/10/07/bit-syntax-for-smalltalk

      3:https://github.com/squaremo/bitsyntax-js

      4:https://gist.github.com/matthewphilyaw/7898f6cb6444246756cd

      5:https://hackage.haskell.org/package/BitSyntax-0.3.2.1/docs/Data-BitSyntax.html

      6:https://www.it.uu.se/research/group/hipe/papers/padl07.pdf

      【讨论】:

        【解决方案5】:

        为什么说它不值钱?

        1> M = <<1, 4, 1, 2, 16#abcd:16>>.                       
        <<1,4,1,2,171,205>>
        2> <<Command:8,Length:8,Parameters/bitstring>> = M.      
        <<1,4,1,2,171,205>>
        3> <<Bit:1,Nible:4,Byte:8,Rest/bitstring>> = Parameters.
        <<1,2,171,205>>
        4> Bit.                                                 
        0
        5> Nible.                                               
        0
        6> Byte.                                                
        32
        7> Rest.                                                
        <<85,121,5:3>>
        8>
        

        【讨论】:

        • Invaluable 的意思是“非常有用;不可或缺。”
        • 所以它是一个新的“坏朋友”,我必须添加到我的列表中,除了危险,控制......:o)
        猜你喜欢
        • 1970-01-01
        • 2011-09-06
        • 1970-01-01
        • 2012-04-10
        • 2015-02-08
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多