Method: CTypes::Bitmap#unpack_one

Defined in:
lib/ctypes/bitmap.rb

#unpack_one(buf, endian: default_endian) ⇒ ::Array

convert a String containing the binary represention of a c type into the equivalent ruby type

Examples:

b = bitmap(uint8, {a: 0, b: 1, c: 7})
b.unpack("\x00")                  # => []
b.unpack("\x01")                  # => [:a]
b.unpack("\x81")                  # => [:a, :c]

allow unlabelled bits in the value

b = bitmap(uint8, {a: 0, b: 1, c: 7})
b.unpack("\x04")                  # => Dry::Types::ConstraintError

# create a new permissive bitmap from the initial bitmap
bp = b.permissive
bp.unpack("\x04")                 # => [:bit_2]

# known bits are still unpacked with the proper name
bp.unpack("\x05")                 # => [:a, :bit_2]

Parameters:

  • buf (::String)

    bytes that make up the type

  • endian (Symbol) (defaults to: default_endian)

    endian of data within buf

Returns:

  • (::Array)

    unpacked bitfield



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/ctypes/bitmap.rb', line 129

def unpack_one(buf, endian: default_endian)
  value, rest = @type.unpack_one(buf, endian: @type.endian || endian)
  bits = []
  @bits_max.times do |bit|
    next if value & (1 << bit) == 0
    v = @bits.dry_type[bit]
    v = :"bit_#{v}" if v.is_a?(Integer)
    bits << v
  end
  [bits, rest]
end