Skip to main content
On this page

@std/bytes

Overview Jump to heading

Helper functions for working with Uint8Array byte slices.

import { concat, indexOfNeedle, endsWith } from "@std/bytes";
import { assertEquals } from "@std/assert";

const a = new Uint8Array([0, 1, 2]);
const b = new Uint8Array([3, 4, 5]);

const c = concat([a, b]);

assertEquals(c, new Uint8Array([0, 1, 2, 3, 4, 5]));

assertEquals(indexOfNeedle(c, new Uint8Array([2, 3])), 2);

assertEquals(endsWith(c, b), true);

Add to your project Jump to heading

deno add jsr:@std/bytes

See all symbols in @std/bytes on

什么是 Uint8Array? Jump to heading

Uint8Array 是一个基于 ArrayBuffer 的类型化数组视图,表示原始字节。每个元素是一个无符号的 8 位整数(0–255)。类型化数组长度固定,效率高,非常适合处理文件、网络、加密、图像、WASM 内存等二进制数据。

何时使用 @std/bytes Jump to heading

如果你正在操作原始二进制数据(Uint8Array),例如搜索、切片、比较或拼接。此包提供了 JavaScript 中没有的有用辅助函数。

示例 Jump to heading

import { concat, equals, indexOfNeedle } from "@std/bytes";

const a = new Uint8Array([1, 2]);
const b = new Uint8Array([3, 4]);
const all = concat([a, b]); // Uint8Array [1,2,3,4]

console.log(equals(all, new Uint8Array([1, 2, 3, 4])));
console.log(indexOfNeedle(all, new Uint8Array([2, 3]))); // 1

小贴士 Jump to heading

  • 为了清晰和正确,优先使用这些辅助函数,而不是手写循环。
  • 使用 TextEncoder/TextDecoder 进行文本 ↔ 字节转换。

你找到了你需要的东西吗?

编辑此页面
隐私政策