パッケージ、リポジトリ、アプリカテゴリを検索

パッケージ詳細v4.0.0

binary

Dart におけるバイナリデータおよびビット操作のためのユーティリティ。

AndroidiOSWebmacOSWindowsLinux
30日間ダウンロード
1.2万
いいね数
24
Pub ポイント
160

パッケージをインストール

flutter pub add binary

パッケージ概要

公開情報に基づく選定ガイドです。参考としてご利用ください。

バイナリパッケージは、Dartでバイナリデータを扱い、ビット単位の操作を行うためのユーティリティを提供します。効率的なバイナリストリームの読み取り・書き込み・操作が可能であり、シフト、マスク、個々のビットの抽出などのビット演算も行えます。低レベルのデータ処理、シリアル化、または画像やネットワークプロトコルのような生のバイナリフォーマットを扱う場合に最適です。このパッケージは軽量で、Android、iOS、Web、macOS、Windows、Linuxなどすべての主要プラットフォームと互換性があります。

用途

  • バイナリファイル形式の解析
  • ネットワークプロトコルハンドラの実装
  • 原始センサー データの処理
  • ビット単位のデータエンコード/デコード
  • バイナリシリアル化の処理
  • カスタムバイナリーコーデックの作成

主な機能

  • 効率的なビット操作
  • ビッグエンディアンおよびリトルエンディアン形式のサポート
  • ストリームベースのバイナリ読み取り/書き込み
  • クロスプラットフォーム互換性
  • 一般的なバイナリ操作向けのシンプルな API
  • 外部の依存関係なし

適している対象

  • バイナリデータを扱う開発者
  • ローレベルシステムプログラミング
  • バイナリ資産を用いたゲーム開発
  • IoT および組み込みシステム
  • ネットワークプロトコルの実装

注意事項

  • 高性能な重要なアプリケーションには適していません
  • バイナリデータの概念を理解する必要がある
  • 基本的なビット演算に限定
  • 複雑なフォーマットに対応するための追加のパースロジックが必要になる可能性があります
  • ストリーミングではない、有限なバイナリデータに最適

元の README

関連するソースリポジトリの README 原文のスナップショットです。最新の内容はリポジトリをご覧ください。

Binary

Utilities for accessing binary data and bit manipulation in Dart.

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

[!IMPORTANT] Version 4.0.0 has a large set of breaking changes, including removing the vast majority of extension methods and boxed classes, in favor of using the newer extension types feature in Dart. I would be opening to adding back some deprecated methods, or a lib/compat.dart file if there is demand; please file an issue if you need this.

Getting started

dart pub add binary

Then, import the package and start using it:

import 'package:binary/binary.dart';

If you are not familiar with extension methods and extension types, 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

  // Old API in version < 4.0.0:
  print(0x0C.toBinaryPadded(8)); // 00001100
}

You now use toBinaryString (and other methods) on an extension type:

void main() {
  // New API.
  print(Uint8(0x0C).toBinaryString()); // 00001100
}

Notice that the width is no longer required, as the type itself knows its width.

Usage

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

Most users will use one of the fixed-width integer types, such as Uint8:

// Parse and reprsent a binary integer:
final rawInt = int.parse('0111' '1111', radix: 2);
final fixInt = Uint8(rawInt);
print(fixInt); // 127

// Works identically to the >>> operator in JavaScript:
final shifted = fixInt.signedRightShift(5);
print(shifted.toBinaryString()); // 00000011
Overflows

Fixed with integers do not overflow unexpectedly, unlike the core Dart types.

By default, any operation that can overflow throws an assertion error in debug mode, and wraps around in release mode. For example:

// An error in debug mode, or 0 in release mode.
Uint8(255) + Uint8(1);

To disable assertions, and always wrap, even in debug mode:

debugCheckFixedWithInRange = false;

Finer-grained control is available by using variants of the operators that explicitly handle overflow:

// Returns null if the operation would overflow.
Uint8(255).tryAdd(Uint8(1));

// Wraps around if the operation would overflow.
Uint8(255).wrappedAdd(Uint8(1));

// Clamps the result to the min/max value if the operation would overflow.
Uint8(255).clampedAdd(Uint8(1));

An additional variant, uncheckedAdd, is available for when you are certain that overflow is not possible, and want to avoid the overhead of checking:

// Asserts in debug mode, no overhead in release mode, but can overflow!

// OK, this will definitely not overflow.
Uint8(1).uncheckedAdd(Uint8(3));

// DANGEROUS: This will overflow, but it won't be checked in release mode!
Uint8(255).uncheckedAdd(Uint8(1));

Compatibility

This package is intended to work identically and well in:

  • The Dart VM, in both JIT and AOT modes.
  • Flutter, in both JIT and AOT modes.
  • The web, in both DDC, Dart2JS modes and compiled to WASM.

As a result, there are no built-in ways to access integers > 32-bit provided (as JS integers are limited).

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

Contributing

To run the tests, run:

dart test

Or, to run a comprehensive set of tests on all supported platforms, run:

dart test -P all

To check code coverage locally, run:

./chore coverage

To preview dartdoc output locally, run:

./chore dartdoc

カテゴリー

依存関係

依存関係一覧 1 件

ソースリポジトリ

このパッケージを使用しているプロジェクト

データはまだありません。