# @1inch/byte-utils 中文文档

规范地址：https://npmcn.com/package/%401inch/byte-utils/

版本：3\.1\.9
npm 元数据更新时间：2026-10-09T15:43:11.728Z
中文译文更新时间：2026-11-05T17:48:14.761Z
软件包许可：MIT

本文由上游 README 翻译，可能存在偏差，请结合原文核对 API、命令与配置。版权与许可归原作者所有。

原始 npm 页面：https://www.npmjs.com/package/%401inch/byte-utils
原文对照：https://npmcn.com/package/%401inch/byte-utils/?lang=original
项目主页：https://github.com/1inch/ts-byte-utils-lib#readme
源码仓库：https://github.com/1inch/ts-byte-utils-lib

## 关键信息（来自 npm 元数据）

- 包体积（解压后）：61.1 kB，59 个文件
- 模块格式：CommonJS
- TypeScript 类型：包内自带
- Node.js 要求：\>=22
- 运行依赖：0 个
- peer 依赖：assert ^2\.0\.0
- 首次发布：2024-03-07T15:24:12.138Z
- 最新版本发布：2026-09-11T07:20:11.260Z
- 近 30 天下载量：137849（2026-10-05 至 2026-11-03，npm 官方下载统计）
- 已发布版本数：25（近 12 个月 2 个）

## 中文简介

包含用于字节工作的助手

## 安装

```sh
npm install @1inch/byte-utils
```

也可以使用：`pnpm add @1inch/byte-utils`、`yarn add @1inch/byte-utils`、`bun add @1inch/byte-utils`

## README

# @ 1英寸/ 字节图象

包含用于字节工作的助手

## 安装

```shell
npm install @1inch/byte-utils
```

## 文档

## :工具箱:函数

-   [是 HexString 语句](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-ishexstring)
-   [是 Hex 键](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-ishexbytes)

### : 齿轮: 是 HexString

检查此字符串从 0x 开始, 并且只有有效的十六进制符号

| 函数 | 类型 |
| --- | --- |
| `isHexString` | `(val: string) => boolean` |

### : gear: 是 HexBytes 语句

检查有 0x 前缀且长度均匀的字符串是有效的十六进制

| 函数 | 类型 |
| --- | --- |
| `isHexBytes` | `(val: string) => boolean` |

## :扳手:常数

-   [UINT 8 地图](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-uint_8_max)
-   [UINT 24 MAX (英语)](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-uint_24_max)
-   [UINT 32 MAX (英语)](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-uint_32_max)
-   [UINT 40 MAX (英语)](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-uint_40_max)
-   [UINT 48 MAX (英语)](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-uint_48_max)
-   [UINT 80 MAX 请检查URL=值 (帮助)](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-uint_80_max)
-   [UINT 160 MAX 数据](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-uint_160_max)
-   [UINT 128 MAX (英语)](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-uint_128_max)
-   [UINT 256 MAX (英语)](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-uint_256_max)

### : 齿轮: UINT 8 MAX

| 常数 | 类型 |
| --- | --- |
| `UINT_8_MAX` | `bigint` |

### : 齿轮: UINT 24 MAX

| 常数 | 类型 |
| --- | --- |
| `UINT_24_MAX` | `bigint` |

### : 齿轮: UINT 32 MAX

| 常数 | 类型 |
| --- | --- |
| `UINT_32_MAX` | `bigint` |

### : 齿轮: UINT 40 MAX

| 常数 | 类型 |
| --- | --- |
| `UINT_40_MAX` | `bigint` |

### : 齿轮: UINT 48 MAX

| 常数 | 类型 |
| --- | --- |
| `UINT_48_MAX` | `bigint` |

### : 齿轮: UINT 80 MAX

| 常数 | 类型 |
| --- | --- |
| `UINT_80_MAX` | `bigint` |

### : 齿轮: UINT 160 MAX

| 常数 | 类型 |
| --- | --- |
| `UINT_160_MAX` | `bigint` |

### : 齿轮: UINT 128 MAX

| 常数 | 类型 |
| --- | --- |
| `UINT_128_MAX` | `bigint` |

### : 齿轮: UINT 256 MAX

| 常数 | 类型 |
| --- | --- |
| `UINT_256_MAX` | `bigint` |

## : 构件: bitMask

用于定义比特掩码的类: 新的 BitMask( 16, 32) 是来自 \[16, 32\] 的比特 0xffff000 (英语)

### 方法

-   [倒数](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-tostring)
-   [切换为](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-tobigint)

#### : 齿轮: to string

| 方法 | 类型 |
| --- | --- |
| `toString` | `() => string` |

#### : gear: to BigInt (中文(简体) )

| 方法 | 类型 |
| --- | --- |
| `toBigInt` | `() => bigint` |

## : 要素: BN

用大数位工作的类 不可改变, 所有方法都返回新值

### 方法

-   [数字](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-fromnumber)
-   [从Hex 调用](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-fromhex)
-   [添加](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-add)
-   [子目录](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-sub)
-   [设置比特](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-setbit)
-   [获取比特](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-getbit)
-   [左转](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-shiftleft)
-   [右转](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-shiftright)
-   [财务报告和审定财务报表](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-and)
-   [或者说](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-or)
-   [十进制](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-xor)
-   [零点](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-iszero)
-   [是一个](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-isone)
-   [获取任务](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-getmask)
-   [设置任务](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-setmask)
-   [清除任务](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-clearmask)
-   [切换](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-tohex)
-   [数字](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-tonumber)

#### : gear: 从数字

| 方法 | 类型 |
| --- | --- |
| `fromNumber` | `(n: number) => BN` |

#### : 齿轮: 从Hex

| 方法 | 类型 |
| --- | --- |
| `fromHex` | `(hex: string) => BN` |

#### : 齿轮: 添加

添加值

| 方法 | 类型 |
| --- | --- |
| `add` | `(other: BN) => BN` |

#### : 齿轮: 子

减值

| 方法 | 类型 |
| --- | --- |
| `sub` | `(other: BN) => BN` |

#### : 齿轮: 设置Bit

| 方法 | 类型 |
| --- | --- |
| `setBit` | `(n: bigint, value: 0 or 1) => BN` |

#### : 齿轮: 得到 Bit

| 方法 | 类型 |
| --- | --- |
| `getBit` | `(n: bigint) => 0 or 1` |

#### : 齿轮: 左转

| 方法 | 类型 |
| --- | --- |
| `shiftLeft` | `(n: bigint) => BN` |

#### : 齿轮: 右移

| 方法 | 类型 |
| --- | --- |
| `shiftRight` | `(n: bigint) => BN` |

#### :齿轮:和

| 方法 | 类型 |
| --- | --- |
| `and` | `(other: bigint or BN) => BN` |

#### : 齿轮:或

| 方法 | 类型 |
| --- | --- |
| `or` | `(other: bigint or BN) => BN` |

#### : 齿轮: xor

| 方法 | 类型 |
| --- | --- |
| `xor` | `(other: bigint or BN) => BN` |

#### : 齿轮: 是 Zero

| 方法 | 类型 |
| --- | --- |
| `isZero` | `() => boolean` |

#### : 齿轮: 是 One

| 方法 | 类型 |
| --- | --- |
| `isOne` | `() => boolean` |

#### : gear: 获取任务

返回位定义于 `mask` 作为BN

| 方法 | 类型 |
| --- | --- |
| `getMask` | `(mask: BitMask) => BN` |

#### : gear: 设置任务

设定定义的位元 `mask` 改为 `value` 如果值大于掩码, 错误将被丢弃

| 方法 | 类型 |
| --- | --- |
| `setMask` | `(mask: BitMask, value: bigint or BN) => BN` |

#### : gear: 清除任务

设定定义的位元 `mask` 改为 0s

| 方法 | 类型 |
| --- | --- |
| `clearMask` | `(mask: BitMask) => BN` |

#### : 齿轮: toHex

返回 0x 前缀字符串并带有 BN 的六进制表示符, 加上 “ 0s ” 如果 `padNum` 指定

| 方法 | 类型 |
| --- | --- |
| `toHex` | `(padNum?: number) => string` |

#### : gear: 到数字

将 BN 转换为编号

注意:数值将四舍五入 > `Number.MAX_SAFE_INTEGER`

| 方法 | 类型 |
| --- | --- |
| `toNumber` | `() => number` |

## : 构件: 字节

通过解析单个字节来通过字节字符串进行斜拉

### 方法

-   [大点](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-bigint)
-   [字符串](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-string)
-   [休养](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-rest)
-   [时空](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-isempty)
-   [下一个字节](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-nextbyte)
-   [下一个字节](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-nextbytes)
-   [下一个地址](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-nextaddress)
-   [下一个Uint8](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-nextuint8)
-   [下一个Uint16](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-nextuint16)
-   [下一个Uint24](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-nextuint24)
-   [下一个Uint32](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-nextuint32)
-   [下一个Uint128](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-nextuint128)
-   [下一个Uint160](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-nextuint160)
-   [下一个Uint256](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-nextuint256)

#### : 齿轮: BigInt

| 方法 | 类型 |
| --- | --- |
| `BigInt` | `(bytes: string) => BytesIter<bigint>` |

#### : gear: 字符串

| 方法 | 类型 |
| --- | --- |
| `String` | `(bytes: string) => BytesIter<string>` |

#### : 齿轮: 休息

返回所有未消耗的字节

| 方法 | 类型 |
| --- | --- |
| `rest` | `() => T` |

#### : 齿轮: 是Empty

| 方法 | 类型 |
| --- | --- |
| `isEmpty` | `() => boolean` |

#### : gear: 下一个Byte

| 方法 | 类型 |
| --- | --- |
| `nextByte` | `(side?: Side) => T` |

#### : gear: 下一个字节

| 方法 | 类型 |
| --- | --- |
| `nextBytes` | `(n: number, side?: Side) => T` |

#### : gear: 下一个地址

| 方法 | 类型 |
| --- | --- |
| `nextAddress` | `(side?: Side) => T` |

#### : gear: 下一个Uint8

| 方法 | 类型 |
| --- | --- |
| `nextUint8` | `(side?: Side) => T` |

#### : gear: 下一个Uint16

| 方法 | 类型 |
| --- | --- |
| `nextUint16` | `(side?: Side) => T` |

#### : gear: 下一个Uint24

| 方法 | 类型 |
| --- | --- |
| `nextUint24` | `(side?: Side) => T` |

#### : gear: 下一个Uint32

| 方法 | 类型 |
| --- | --- |
| `nextUint32` | `(side?: Side) => T` |

#### : gear: 下一个Uint128

| 方法 | 类型 |
| --- | --- |
| `nextUint128` | `(side?: Side) => T` |

#### : gear: 下一个Uint160

| 方法 | 类型 |
| --- | --- |
| `nextUint160` | `(side?: Side) => T` |

#### : 齿轮: 下个Uint256

| 方法 | 类型 |
| --- | --- |
| `nextUint256` | `(side?: Side) => T` |

## : 构件: 字节构建器

构建任意字节序列的帮助类

### 方法

-   [添加语句](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-addaddress)
-   [添加字节](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-addbytes)
-   [添加键](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-addbyte)
-   [添加视图8](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-adduint8)
-   [添加Uint16 数据](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-adduint16)
-   [添加视图24](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-adduint24)
-   [添加Uint32](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-adduint32)
-   [添加Uint64 数据](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-adduint64)
-   [添加Uint128 数据](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-adduint128)
-   [添加Uint160 数据](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-adduint160)
-   [加图256](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-adduint256)
-   [作为 BigInt](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-asbigint)
-   [作为Hex](https://npmcn.com/package/%401inch/byte-utils/#readme-gear-ashex)

#### : 设备: 添加添加

| 方法 | 类型 |
| --- | --- |
| `addAddress` | `(address: string or bigint or BN) => this` |

#### : gear: 添加字节

| 方法 | 类型 |
| --- | --- |
| `addBytes` | `(bytes: string) => this` |

#### : gear: 添加键

| 方法 | 类型 |
| --- | --- |
| `addByte` | `(byte: string or bigint or BN) => this` |

#### : 齿轮: 添加Uint8

| 方法 | 类型 |
| --- | --- |
| `addUint8` | `(val: string or bigint or BN) => this` |

#### : 齿轮: 添加Uint16

| 方法 | 类型 |
| --- | --- |
| `addUint16` | `(val: string or bigint or BN) => this` |

#### : 齿轮: 添加Uint24

| 方法 | 类型 |
| --- | --- |
| `addUint24` | `(val: string or bigint or BN) => this` |

#### : 齿轮: 添加Uint32

| 方法 | 类型 |
| --- | --- |
| `addUint32` | `(val: string or bigint or BN) => this` |

#### : 齿轮: 添加Uint64

| 方法 | 类型 |
| --- | --- |
| `addUint64` | `(val: string or bigint or BN) => this` |

#### : 齿轮: 添加Uint128

| 方法 | 类型 |
| --- | --- |
| `addUint128` | `(val: string or bigint or BN) => this` |

#### : 齿轮: 添加Uint160

| 方法 | 类型 |
| --- | --- |
| `addUint160` | `(val: string or bigint or BN) => this` |

#### : 齿轮: 添加Uint256

| 方法 | 类型 |
| --- | --- |
| `addUint256` | `(val: string or bigint or BN) => this` |

#### : gear: as BigInt

以单大值返回字节

| 方法 | 类型 |
| --- | --- |
| `asBigInt` | `() => bigint` |

#### : 齿轮: asHex

返回十六进制字符串

| 方法 | 类型 |
| --- | --- |
| `asHex` | `(prefixed?: boolean) => string` |

参数:

-   `prefixed`: 字符串前缀为 0x 与否, 默认为真实

## 常见问题

### @1inch/byte-utils 是什么？

包含用于字节工作的助手

### 如何安装 @1inch/byte-utils？

在项目目录运行 npm install @1inch/byte-utils，或使用 pnpm add / yarn add / bun add。

### @1inch/byte-utils 支持 TypeScript 吗？

支持。包内自带 TypeScript 类型声明（package\.json 声明了 types 入口），安装后即可获得类型提示，无需再安装 @types 包。

### @1inch/byte-utils 支持 ESM 还是 CommonJS？

以 CommonJS 形式发布，可以直接 require\(\)；在 ES Module 项目中也可以用 import 引入（Node\.js 会把 module\.exports 作为默认导出）。

### @1inch/byte-utils 需要什么版本的 Node\.js？

package\.json 的 engines 字段要求 Node\.js \>=22。版本不符时 npm 默认只给出警告，开启 engine-strict 后会拒绝安装。

### @1inch/byte-utils 可以商用吗？

@1inch/byte-utils 使用 MIT 许可。宽松许可：允许商业使用、修改和再分发，通常只需在分发时保留原版权与许可声明。以上为概括，不构成法律意见，请以许可原文为准。

### @1inch/byte-utils 的下载量有多少？

根据 npm 官方下载统计，@1inch/byte-utils 在 2026/10/05 – 2026/11/03 这 30 天内被下载 137,849 次，日均约 4,595 次。下载量会把 CI 构建、镜像同步和作为其他包依赖的安装一并计入，适合横向比较热度，不能直接等同于使用人数。

### @1inch/byte-utils 还在维护吗？

最新版本 3\.1\.9 发布于 2026/09/11（1 个月前）；至今共发布 25 个版本，其中过去 12 个月发布了 2 个；首次发布于 2024/03/07。发布频率只是参考信号：功能稳定的工具可能很少需要更新，建议结合源码仓库的 issue 和提交记录判断。

