使用Ruby BinData gem来读取选择

Ind*_*a K 1 ruby choice binary-data bindata

我正在使用Ruby和BinData gem 实现数据结构.我需要实现一个Choice值.根据BinData文档,可以选择实现:

class MyData < BinData::Record
  uint8  :type
  choice :data, :selection => :type do
    type key #option 1
    type key #option 2
  end
end
Run Code Online (Sandbox Code Playgroud)

我需要在选择中有一个默认选项:

class MyRecord < BinData::Record
    uint8 :type
    choice :mydata, :selection => :type do
            uint32 0
            uint16 1
    end
end
Run Code Online (Sandbox Code Playgroud)

如果type不是01在上面的代码中,如何处理?

小智 5

BinData 1.4.1本地处理它 :default

class MyRecord < BinData::Record
  uint8 :data_type
  choice :mydata, :selection => :data_type do
    uint32 1
    uint16 2
    string :default, :read_length => 4
  end
end
Run Code Online (Sandbox Code Playgroud)