搜索插件、代码仓与 APP 分类

插件详情v4.0.0

binary

用于在 Dart 中访问二进制数据和位操作的工具。

AndroidiOSWebmacOSWindowsLinux
近 30 天下载
1.2万
喜欢数
24
Pub 分数
160

安装插件

flutter pub add binary

插件介绍

基于插件公开信息、社区数据与项目使用情况整理的选型参考。

该二进制包提供用于处理二进制数据和执行位级操作的工具。它支持高效地读取、写入和操作二进制流,以及执行位运算,如移位、掩码和提取单个位。非常适合低级别数据处理、序列化或处理原始二进制格式(如图像或网络协议)。该包设计轻量,可在所有主要平台(包括 Android、iOS、Web、macOS、Windows 和 Linux)上兼容。

适合场景

  • 解析二进制文件格式
  • 实现网络协议处理程序
  • 处理原始传感器数据
  • 位运算数据编码/解码
  • 处理二进制序列化
  • 创建自定义二进制编码器

主要特点

  • 高效的位操作
  • 对大端和小端格式的支持
  • 基于流的二进制读写
  • 跨平台兼容性
  • 用于常见二进制操作的简单 API
  • 无外部依赖

适合对象

  • 处理二进制数据的开发者
  • 低级系统编程
  • 使用二进制资源进行游戏开发
  • 物联网和嵌入式系统
  • 网络协议实现

使用建议

  • 不适用于高性能关键应用
  • 需要理解二进制数据概念
  • 仅限基本位操作
  • 可能需要额外的解析逻辑来处理复杂格式
  • 最适合非流式、有限二进制数据

原始 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 项

源码仓库

使用此插件的项目

暂无相关数据。