# @guanghechen/byte

> Utility functions for bytes (Uint8Array).

Latest version **2.0.5** (published 2026-09-12) · MIT license · 0 weekly downloads

## Install

```sh
npm install @guanghechen/byte
pnpm add @guanghechen/byte
yarn add @guanghechen/byte
bun add @guanghechen/byte
```

## Health

**Score 70/100 (B)** — status: active.

Positive: has types; esm support; no vulnerabilities; recently updated; high maintenance score; high quality score.

Warnings: low downloads.

## Facts

| | |
|---|---|
| Version | 2.0.5 |
| Published | 2026-09-12 |
| First published | 2023-10-22 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 0 |
| Unpacked size | 22.4 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 0 |
| Author | guanghechen |
| Maintainers | lemonclown |
| Keywords | Uint8Array |

## Links

- npm: https://www.npmjs.com/package/@guanghechen/byte
- Repository: https://github.com/guanghechen/sora/tree/@guanghechen/byte@2.0.5
- Homepage: https://github.com/guanghechen/sora/tree/@guanghechen/byte@2.0.5/packages/byte#readme
- npm.io page: https://npm.io/package/@guanghechen/byte

## Recent versions

- 2.0.5 (latest) — 2026-09-12
- 2.0.4 — 2026-07-10
- 2.0.3 — 2026-06-27
- 2.0.2 — 2026-02-15
- 2.0.1 — 2026-02-07
- 2.0.0 — 2026-02-04
- 1.0.3 — 2024-10-03
- 1.0.2 — 2024-09-28
- 1.0.1 — 2024-09-25
- 1.0.0 — 2024-09-20
- 1.0.0-alpha.7 — 2024-09-18
- 1.0.0-alpha.6 — 2024-02-03
- 1.0.0-alpha.5 — 2024-01-02
- 1.0.0-alpha.4 — 2023-11-29
- 1.0.0-alpha.3 — 2023-11-18
- … 2 more at https://npm.io/package/@guanghechen/byte/versions

## README

<header>
  <h1 align="center">
    <a href="https://github.com/guanghechen/sora/tree/@guanghechen/byte@2.0.5/packages/byte#readme">@guanghechen/byte</a>
  </h1>
  <div align="center">
    <a href="https://www.npmjs.com/package/@guanghechen/byte">
      <img
        alt="Npm Version"
        src="https://img.shields.io/npm/v/@guanghechen/byte.svg"
      />
    </a>
    <a href="https://www.npmjs.com/package/@guanghechen/byte">
      <img
        alt="Npm Download"
        src="https://img.shields.io/npm/dm/@guanghechen/byte.svg"
      />
    </a>
    <a href="https://www.npmjs.com/package/@guanghechen/byte">
      <img
        alt="Npm License"
        src="https://img.shields.io/npm/l/@guanghechen/byte.svg"
      />
    </a>
    <a href="#install">
      <img
        alt="Module Formats: cjs"
        src="https://img.shields.io/badge/module_formats-cjs-green.svg"
      />
    </a>
    <a href="https://github.com/nodejs/node">
      <img
        alt="Node.js Version"
        src="https://img.shields.io/node/v/@guanghechen/byte"
      />
    </a>
    <a href="https://github.com/facebook/jest">
      <img
        alt="Tested with Jest"
        src="https://img.shields.io/badge/tested_with-jest-9c465e.svg"
      />
    </a>
    <a href="https://biomejs.dev/">
      <img
        alt="Code Style: Biome"
        src="https://img.shields.io/badge/code_style-biome-60a5fa.svg?style=flat-square"
      />
    </a>
  </div>
</header>
<br/>

Utility functions for bytes (Uint8Array). Provides encoding/decoding between bytes and various text
formats (base64, hex, utf8), along with common byte manipulation utilities.

## Install

- npm

  ```bash
  npm install --save @guanghechen/byte
  ```

- yarn

  ```bash
  yarn add @guanghechen/byte
  ```

## Usage

### Encoding / Decoding

```typescript
import {
  text2bytes,
  bytes2text,
  base64Text2bytes,
  bytes2base64Text,
  hexText2bytes,
  bytes2hexText,
  utf8Text2bytes,
  bytes2utf8Text,
} from '@guanghechen/byte'

// Generic encoding/decoding with encoding type
const bytes = text2bytes('Hello World', 'utf8')
const text = bytes2text(bytes, 'utf8') // 'Hello World'

// Base64
const base64Bytes = base64Text2bytes('SGVsbG8gV29ybGQ=')
const base64Text = bytes2base64Text(new Uint8Array([72, 101, 108, 108, 111])) // 'SGVsbG8='

// Hex
const hexBytes = hexText2bytes('48656c6c6f')
const hexText = bytes2hexText(new Uint8Array([72, 101, 108, 108, 111])) // '48656c6c6f'

// UTF-8
const utf8Bytes = utf8Text2bytes('Hello')
const utf8Text = bytes2utf8Text(utf8Bytes) // 'Hello'
```

### Byte Manipulation

```typescript
import {
  mergeBytes,
  areSameBytes,
  randomBytes,
  destroyBytes,
} from '@guanghechen/byte'

// Merge multiple Uint8Arrays into one
const merged = mergeBytes([
  new Uint8Array([1, 2]),
  new Uint8Array([3, 4]),
]) // Uint8Array [1, 2, 3, 4]

// Compare two Uint8Arrays
const isEqual = areSameBytes(
  new Uint8Array([1, 2, 3]),
  new Uint8Array([1, 2, 3]),
) // true

// Generate cryptographically random bytes
const random = randomBytes(16) // 16 random bytes

// Securely destroy sensitive byte data
const sensitiveData = new Uint8Array([1, 2, 3, 4])
destroyBytes(sensitiveData) // Overwrites with zeros, ones, then random values
```

## Reference

- [homepage][homepage]

[homepage]:
  https://github.com/guanghechen/sora/tree/@guanghechen/byte@2.0.5/packages/byte#readme

---
_Source: https://npm.io/package/@guanghechen/byte · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
