binary library

Utilities for working with binary data within Dart.

NOTE: Unless otherwise noted, all functionality is based around treating bits as little endian, that is, in a 32-bit integer the leftmost bit is 31 and the rightmost bit is 0.

There are a few sets extension methods that are intended to be generally useful for libraries and apps that need to access, manipulate, or visualize binary data (and individual bits), and are intended to be as performant as possible:

  • BinaryInt: Provides int with methods to access/manipulate bytes.
  • BinaryList: Assumes a List<int> of just 0 and 1, provides methods.
  • BinaryString: Assumes a String of just '0' and 1, provides methods.

Do note that the built-in dart:typed_data representations, such as Uint8List are greatly preferred in terms of performance to creating your own abstractions like List<int>. Extensions similar to BinaryInt are also provided for the various typed list sub-types:

Notably, the above extension methods do not know the underlying bit size and require a length parameter where the method would otherwise be ambiguous.

For users that desire more type safety (i.e. want to explicitly declare and enforce size of binary data) at the cost of performance, there are also boxed int representations:

Integers with a size greater than 32-bits are not explicitly supported due to the fact that compatibility varies based on the deployment platform (e.g. on the web/JavaScript).

We could add limited forms of support with BigInt; file a request!

Classes

Bit
Encapsulates a single (unsigned) bit, i.e. either 0 or 1.
BitPart
Part of a BitPattern that will be used to match.
BitPattern<T>
Represents the result of calling BitPatternBuilder.build.
BitPatternBuilder
Builds a sequence of binary digits.
BitPatternGroup<T, V extends BitPattern<T>>
Allows matching integer values against a collection of BitPatterns.
Int16
Encapsulates a signed 16-bit aggregation.
Int32
Encapsulates a signed 32-bit aggregation.
Int4
Encapsulates a signed 4-bit aggregation.
Int8
Encapsulates a signed 8-bit aggregation.
Integral<T extends Integral<T>>
Encapsulates a common integral data type of declared size and signs.
Uint16
Encapsulates an unsigned 16-bit aggregation.
Uint32
Encapsulates an unsigned 32-bit aggregation.
Uint4
Encapsulates an unsigned 4-bit aggregation.
Uint8
Encapsulates an unsigned 8-bit aggregation.

Extensions

BinaryInt on int
A collection of unchecked binary methods to be applied to a 32-bit int.
BinaryInt16List on Int16List
A collection of binary methods to be applied to elements of Int16List.
BinaryInt32List on Int32List
A collection of binary methods to be applied to elements of Int32List.
BinaryInt8List on Int8List
A collection of binary methods to be applied to elements of Int8List.
BinaryList on List<int>
A collection of binary methods to be applied to any List<int> instance.
BinaryString on String
A collection of binary methods to be applied to any String instance.
BinaryUint16List on Uint16List
A collection of binary methods to be applied to elements of Uint16List.
BinaryUint32List on Uint32List
A collection of binary methods to be applied to elements of Uint32List.
BinaryUint64HiLo on Uint32List
A collection of binary methods to be applied to elements of Uint32List.
BinaryUint8List on Uint8List
A collection of binary methods to be applied to elements of Uint8List.