@1inch/byte-utils

中文文档
3.1.9发布于 2026/09/11MITTypeScriptCommonJS零依赖构建来源已验证近 30 天下载 13.8万

Contains helpers to work with bytes

原始使用文档
文档目录23 节
  1. Installation
  2. Docs
  3. :toolbox: Functions
  4. :gear: isHexString
  5. :gear: isHexBytes
  6. :wrench: Constants
  7. :gear: UINT_8_MAX
  8. :gear: UINT_24_MAX
  9. :gear: UINT_32_MAX
  10. :gear: UINT_40_MAX
  11. :gear: UINT_48_MAX
  12. :gear: UINT_80_MAX
  13. :gear: UINT_160_MAX
  14. :gear: UINT_128_MAX
  15. :gear: UINT_256_MAX
  16. :factory: BitMask
  17. Methods
  18. :factory: BN
  19. Methods
  20. :factory: BytesIter
  21. Methods
  22. :factory: BytesBuilder
  23. Methods

@1inch/byte-utils

Contains helpers to work with bytes

Installation

npm install @1inch/byte-utils

Docs

:toolbox: Functions

:gear: isHexString

Check that string starts with 0x and has only valid hex symbols

FunctionType
isHexString(val: string) => boolean

:gear: isHexBytes

Check that string is valid hex with 0x prefix and length is even

FunctionType
isHexBytes(val: string) => boolean

:wrench: Constants

:gear: UINT_8_MAX

ConstantType
UINT_8_MAXbigint

:gear: UINT_24_MAX

ConstantType
UINT_24_MAXbigint

:gear: UINT_32_MAX

ConstantType
UINT_32_MAXbigint

:gear: UINT_40_MAX

ConstantType
UINT_40_MAXbigint

:gear: UINT_48_MAX

ConstantType
UINT_48_MAXbigint

:gear: UINT_80_MAX

ConstantType
UINT_80_MAXbigint

:gear: UINT_160_MAX

ConstantType
UINT_160_MAXbigint

:gear: UINT_128_MAX

ConstantType
UINT_128_MAXbigint

:gear: UINT_256_MAX

ConstantType
UINT_256_MAXbigint

:factory: BitMask

Class to define bit mask: new BitMask(16, 32) is for bits from [16, 32) => 0xffff0000

Methods

:gear: toString

MethodType
toString() => string

:gear: toBigInt

MethodType
toBigInt() => bigint

:factory: BN

Class to work with bits in bignumber Immutable, all methods return new value

Methods

:gear: fromNumber

MethodType
fromNumber(n: number) => BN

:gear: fromHex

MethodType
fromHex(hex: string) => BN

:gear: add

Add value

MethodType
add(other: BN) => BN

:gear: sub

Subtract value

MethodType
sub(other: BN) => BN

:gear: setBit

MethodType
setBit(n: bigint, value: 0 or 1) => BN

:gear: getBit

MethodType
getBit(n: bigint) => 0 or 1

:gear: shiftLeft

MethodType
shiftLeft(n: bigint) => BN

:gear: shiftRight

MethodType
shiftRight(n: bigint) => BN

:gear: and

MethodType
and(other: bigint or BN) => BN

:gear: or

MethodType
or(other: bigint or BN) => BN

:gear: xor

MethodType
xor(other: bigint or BN) => BN

:gear: isZero

MethodType
isZero() => boolean

:gear: isOne

MethodType
isOne() => boolean

:gear: getMask

Return bits defined in mask as BN

MethodType
getMask(mask: BitMask) => BN

:gear: setMask

Set bits defined in mask to value if value is bigger than mask then error will be thrown

MethodType
setMask(mask: BitMask, value: bigint or BN) => BN

:gear: clearMask

Set bits defined in mask to 0s

MethodType
clearMask(mask: BitMask) => BN

:gear: toHex

Return 0x prefixed string with hex representation of BN, padded with '0s' if padNum specified

MethodType
toHex(padNum?: number) => string

:gear: toNumber

Convert BN to Number

Caution: value will be rounded for numbers > Number.MAX_SAFE_INTEGER

MethodType
toNumber() => number

:factory: BytesIter

Class to iterate through bytes string by parsing individual bytes

Methods

:gear: BigInt

MethodType
BigInt(bytes: string) => BytesIter<bigint>

:gear: String

MethodType
String(bytes: string) => BytesIter<string>

:gear: rest

Returns all not consumed bytes

MethodType
rest() => T

:gear: isEmpty

MethodType
isEmpty() => boolean

:gear: nextByte

MethodType
nextByte(side?: Side) => T

:gear: nextBytes

MethodType
nextBytes(n: number, side?: Side) => T

:gear: nextAddress

MethodType
nextAddress(side?: Side) => T

:gear: nextUint8

MethodType
nextUint8(side?: Side) => T

:gear: nextUint16

MethodType
nextUint16(side?: Side) => T

:gear: nextUint24

MethodType
nextUint24(side?: Side) => T

:gear: nextUint32

MethodType
nextUint32(side?: Side) => T

:gear: nextUint128

MethodType
nextUint128(side?: Side) => T

:gear: nextUint160

MethodType
nextUint160(side?: Side) => T

:gear: nextUint256

MethodType
nextUint256(side?: Side) => T

:factory: BytesBuilder

Helper class to build an arbitrary bytes sequence

Methods

:gear: addAddress

MethodType
addAddress(address: string or bigint or BN) => this

:gear: addBytes

MethodType
addBytes(bytes: string) => this

:gear: addByte

MethodType
addByte(byte: string or bigint or BN) => this

:gear: addUint8

MethodType
addUint8(val: string or bigint or BN) => this

:gear: addUint16

MethodType
addUint16(val: string or bigint or BN) => this

:gear: addUint24

MethodType
addUint24(val: string or bigint or BN) => this

:gear: addUint32

MethodType
addUint32(val: string or bigint or BN) => this

:gear: addUint64

MethodType
addUint64(val: string or bigint or BN) => this

:gear: addUint128

MethodType
addUint128(val: string or bigint or BN) => this

:gear: addUint160

MethodType
addUint160(val: string or bigint or BN) => this

:gear: addUint256

MethodType
addUint256(val: string or bigint or BN) => this

:gear: asBigInt

Returns bytes as single bigint value

MethodType
asBigInt() => bigint

:gear: asHex

Returns hex string

MethodType
asHex(prefixed?: boolean) => string

Parameters:

  • prefixed: should be string prefixed with 0x or not, true by default

版本与发布记录

@1inch/byte-utils 共发布 25 个版本,首次发布于 2024/03/07;过去 12 个月发布了 2 个版本。最新版本附带 npm provenance 构建来源证明,可追溯到公开的 CI 构建记录。

版本发布日期距今
3.1.9latest2026/09/111 个月前
3.1.82026/08/312 个月前
3.1.72025/10/301 年前
3.1.62025/10/301 年前
3.1.52025/10/301 年前
3.1.42025/10/301 年前

关于 @1inch/byte-utils 的常见问题

@1inch/byte-utils 是什么?

Contains helpers to work with bytes

如何安装 @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 和提交记录判断。