All Projects → matanlurey → Binary.dart

matanlurey / Binary.dart

Licence: mit
Utilities for working with binary data and bit manipulation in Dart.

Programming Languages

dart
5743 projects

Projects that are alternatives of or similar to Binary.dart

Dark Mode
Control the macOS dark mode from the command-line
Stars: ✭ 518 (+3137.5%)
Mutual labels:  binary
Pbf
A low-level, lightweight protocol buffers implementation in JavaScript.
Stars: ✭ 618 (+3762.5%)
Mutual labels:  binary
Lambda Packages
Various popular python libraries, pre-compiled to be compatible with AWS Lambda
Stars: ✭ 713 (+4356.25%)
Mutual labels:  binary
Handmade Math
A simple math library for games and computer graphics. Compatible with both C and C++.
Stars: ✭ 517 (+3131.25%)
Mutual labels:  math
Swix
Swift Matrix Library
Stars: ✭ 581 (+3531.25%)
Mutual labels:  math
Tutorials
Ipython notebooks for math and finance tutorials
Stars: ✭ 654 (+3987.5%)
Mutual labels:  math
Mathc
Pure C math library for 2D and 3D programming
Stars: ✭ 504 (+3050%)
Mutual labels:  math
Pragmatapro
PragmataPro font is designed to help pros to work better
Stars: ✭ 887 (+5443.75%)
Mutual labels:  math
Garble
Obfuscate Go builds
Stars: ✭ 617 (+3756.25%)
Mutual labels:  binary
Scodec
Scala combinator library for working with binary data
Stars: ✭ 709 (+4331.25%)
Mutual labels:  binary
Nimble parsec
A simple and fast library for text-based parser combinators
Stars: ✭ 532 (+3225%)
Mutual labels:  binary
Angr
A powerful and user-friendly binary analysis platform!
Stars: ✭ 5,542 (+34537.5%)
Mutual labels:  binary
Mdmath
LaTeX Math for Markdown inside of Visual Studio Code.
Stars: ✭ 675 (+4118.75%)
Mutual labels:  math
Libertinus
The Libertinus font family
Stars: ✭ 518 (+3137.5%)
Mutual labels:  math
Long.js
A Long class for representing a 64-bit two's-complement integer value.
Stars: ✭ 719 (+4393.75%)
Mutual labels:  math
Android interviews
🚀Everything you need to know to find a android job. 算法 / 面试题 / Android 知识点 🔥🔥🔥 总结不易,你的 star 是我最大的动力!
Stars: ✭ 510 (+3087.5%)
Mutual labels:  binary
Primesieve
🚀 Fast prime number generator
Stars: ✭ 644 (+3925%)
Mutual labels:  math
Cglm
📽 Highly Optimized Graphics Math (glm) for C
Stars: ✭ 887 (+5443.75%)
Mutual labels:  math
Expr Eval
Mathematical expression evaluator in JavaScript
Stars: ✭ 752 (+4600%)
Mutual labels:  math
Symengine
SymEngine is a fast symbolic manipulation library, written in C++
Stars: ✭ 703 (+4293.75%)
Mutual labels:  math

Binary

Utilities for accessing binary data and bit manipulation in Dart and Flutter.

Binary on pub.dev Code coverage Github action status Dartdocs Style guide

Getting started

Using package:binary is easy, we have almost no dependencies:

# Add a new dependency to "pubspec.yaml".
dependencies:
  binary:
import 'package:binary/binary.dart';

// Start using package:binary.

If you are not familiar with extension methods in Dart, it is worth reading the documentation before using this package, which has heavy use of extensions for most functionality. A small primer is instead of writing something like:

void main() {
  // Old API in version <= 0.1.3:
  print(toBinaryPadded(0x0C, 8)); // 00001100
}

You now use toBinaryPadded (and other methods) as an extension method:

void main() {
  // New API.
  print(0x0C.toBinaryPadded(8)); // 00001100
}

Usage

This package provides a few sets of APIs extension methods and boxed classes.

See the API docs for complete documentation.

Most users will use the extension methods on int or String:

// Uses "parseBits" (on String) and "shiftRight" (on int).
void main() {
  test('shiftRight should work identical to >>> in JavaScript', () {
    expect(
      '0111' '1111'.bits.shiftRight(5, 8),
      '0000' '0011'.bits,
    );
  });
}

For convenience, extension methods are also present on List<int>:

// Uses "rotateRight" (on List<int>).
void main() {
  test('rotateRight should work similarly to int.rotateRight', () {
    final list = ['0110' '0000'.bits];
    expect(
      list.rotateRight(0, 1).toBinaryPadded(8),
      '0011' '0000',
    );
  });
}

There are also some specialized extension methods on the typed_data types:

  • Uint8List, Int8List
  • Uint16List, Int16List
  • Uint32List, Int32List

Boxed Types

It is possible to sacrifice performance in order to get more type safety and range checking. For apps or libraries where this is a suitable tradeoff, we provide these boxed types/classes:

  • Bit
  • Int4 and Uint4
  • Int8 and Uint8
  • Int16 and Uint16
  • Int32 and Uint32

Bit Patterns

There is also builder-type API for generating patterns to match against bits. The easiest way to explain this API is it is like RegExp, except for matching and capturing components of bits:

void main() {
  // Create a BitPattern.
  final $01V = BitPatternBuilder([
    BitPart.zero,
    BitPart.one,
    BitPart.v(1),
  ]).build();

  // Match it against bits.
  print($01V.matches('011'.bits)); // true

  // Capture variables (if any), similar to a RegExp.
  print($01V.capture('011'.bits)); // [1]
}

Compatibility

This package is intended to work identically and well in both the standalone Dart VM, Flutter, and web builds of Dart and Flutter (both in DDC and Dart2JS). As a result, there are no built-in ways to access integers > 32-bit provided (as web integers are limited).

Feel free to file an issue if you'd like limited support for 64 and 128-bit.

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].