Table Of Contents

BitField

class BitField(name, size, value=0)

Bases: BitPacket.Field.Field

This class represents bit fields to be used by BitStructure in order to build byte-aligned fields. Remember that BitPacket only works with byte-aligned fields, so it is not possible to create mixed (bit and byte) fields, that’s why BitField can only be used inside a BitStructure.

Initialize the field with the given name and size (in bits). By default the field’s value will be initialized to 0 or to value if specified.

set_value(value)

Sets a new unsigned integer value to the field.

size()

Returns the size of the field in bits.

str_eng_value()

Returns a human-readable representation of the engineering value. This function will first calculate the engineering value (by applying the calibration curve) and will return the string representation of it. In case of bit fields the representation is an hexadecimal value.

str_hex_value()

Returns a human-readable representation of the hexadecimal value of this field. This will return the same as str_value.

str_value()

Returns a human-readable representation of the value of this field. In case of bit fields the representation is an hexadecimal value.

value()

Returns the value of this field. As single bit fields do not have a concrete type (signed integers, float...) this will return the unsigned integer representation of this field.

Container

class Container(name)

Bases: BitPacket.Field.Field

This is an abstrat class to create containers. A Container is just a field that might contain a sequence of fields (that can be containers as well), thus forming a bigger field.

Initialize the Container with the given name. By default, it does not contain any fields.

append(field)

Appends a new field into the Container. A NameError exception will be raised if a field with the same name is already in the container.

field(name)

Returns the field identified by name. name accepts a dot (.) separator to indicate sub-fields of a container (multiple separators are allowed). If the field does not exist a KeyError exception is raised.

fields()

Returns the (ordered) list of fields of this Container.

keys()

Returns the list of fields’ names recursively (i.e. if fields are also containers). In case one or more of the fields are containers, its fields will be suffixed with a dot separator. As an example, “a.b.c” is the key for a field c inside a b container which is also inside a root a container.

reset()

Remove all the fields from this Container.

size()

Returns the size of the field in bytes. That is, the sum of all byte sizes of the fields in this Container.

MetaField

class MetaField(name, fieldfunc)

Bases: BitPacket.Field.Field

type()

String

class String(name, length)

Bases: BitPacket.Field.Field

A String field lets you store a string of characters of any size. Usually, to unpack a string we need to extract the length of the string from another field, in which case we need to specify a function that will know where to get the length from. However, it is also possible to specify a fixed length string.

Initialize the string field with a name and a length. length can be a fixed number or a single arument function that returns the length of the string. The single argument is a reference to the top-level root Container field where the string belongs to. A possible function could be:

lambda root: root["Length"]

where we get the length of the string from a Length field.

set_value(data)

Sets a new string of characters to the field.

size()

Returns the size in bytes of the string.

str_eng_value()

This is equivalent of calling str_value ().

str_hex_value()

This is equivalent of calling str_value ().

str_value()

Returns a text string with the hexadecimal value of each character of the string. A prefix of 0x is added. So, for “hello”, “0x68656C6C6F” would be returned.

value()

Returns the string of characters.

class Text(name, length)

Bases: BitPacket.String.String

Text is basically a String but conceived to be used with only text strings. It does not perform any check on the data. It simply returns the internal string (which should be text) instead of generating a text string with the hexadecimal values of the string.

str_eng_value()

This is equivalent of calling str_value ().

str_hex_value()

This is equivalent of calling str_value ().

str_value()

Returns the text string.

Value

class Value(name, format, value)

Bases: BitPacket.Field.Field

This is the base class for numeric fields. Internally, it uses Python’s struct module to define the numeric value size and the byte order (little-endian or big-endian).

Initialize the field with the given name and value. The format is a string conforming the Python’s struct module format strings.

hex_value()

Returns the hexadecimal integer representation of this field. That is, the bytes forming this field in its integer representation. This will vary depending on the field’s endiannes and size, so UInt16LE will return a different hexadecimal value than UInt16BE for the same number.

set_value(value)

Sets the new numeric value to this field. The value must fit in this field, otherwise an exception is raised.

size()

Returns the size in bytes of this field.

str_eng_value()

Returns a human-readable representation of the engineering value. This function will first calculate the engineering value (by applying the calibration curve) and will return the string representation of it.

str_hex_value()

Returns a human-readable representation of the hexadecimal representation of this field. This internally uses hex_value().

str_value()

Returns a human-readable representation of the numeric value of this field.

value()

Returns the numeric value of this field.