npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

@stassi/binary-transcoder

v0.11.2

Published

Convert binary data between common formats and encodings.

Downloads

38

Readme

@stassi/binary-transcoder

version license types engines code size minified size minzipped size package health Continuous integration Security

Convert binary data between any two formats and encodings listed here.

ArrayLike

Further information: Uint8Array (MDN)

  • number[]
  • Uint8Array

Buffer

Further information: node:buffer (Node.js)

  • Buffer (browser-compatible, Node.js not required)

number

Further information: Number (MDN)

  • number

string

Further information: Binary number (Wikipedia) | JSON (MDN) | node:buffer character encodings (Node.js) | Unicode (Wikipedia)

  • 'base64'
  • 'base64url'
  • 'binary' (binary number string, not the legacy Node.js alias of the same name for 'latin1' encoding)
  • 'hex'
  • 'json'
  • 'latin1'
  • 'utf8' (encode output only)
  • 'utf16le' (encode output only)

Demo

Instant demonstration: @stassi/binary-transcoder (RunKit + npm)

Installation

Node.js

npm i @stassi/binary-transcoder

Usage

Node.js

ES module

import {
  fromBase64,
  fromBase64URL,
  fromBinary,
  fromHex,
  fromJSON,
  fromLatin1,
  transcode,
} from '@stassi/binary-transcoder'

CommonJS

const {
  fromBase64,
  fromBase64URL,
  fromBinary,
  fromHex,
  fromJSON,
  fromLatin1,
  transcode,
} = require('@stassi/binary-transcoder')

Web

import {
  fromBase64,
  fromBase64URL,
  fromBinary,
  fromHex,
  fromJSON,
  fromLatin1,
  transcode,
} from 'https://cdn.skypack.dev/@stassi/binary-transcoder'

Examples

Base64 encoding

transcode([0x3e, 0x3f, 0xfe, 0xff]).toBase64()
// 'Pj/+/w=='

Base64 decoding

fromBase64('Pj/+/w==').toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>
transcode({
  encoding: 'base64',
  text: 'Pj/+/w==',
}).toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>

Base64URL encoding

transcode([0x3e, 0x3f, 0xfe, 0xff]).toBase64URL()
// 'Pj_-_w'

Base64URL decoding

fromBase64URL('Pj_-_w').toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>
transcode({
  encoding: 'base64url',
  text: 'Pj_-_w',
}).toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>

Binary encoding

transcode([0b1001011, 0b1100101, 0b1111001]).toBinary()
// '010010110110010101111001'

Binary decoding

fromBinary('010010110110010101111001').toUInt8Array()
// Uint8Array <4B, 65, 79>
transcode({
  encoding: 'binary',
  text: '010010110110010101111001',
}).toUInt8Array()
// Uint8Array <4B, 65, 79>

Buffer encoding

fromHex('4b6579').toBuffer()
// Buffer <4B, 65, 79>
transcode({
  encoding: 'hex',
  text: '4b6579',
}).toBuffer()
// Buffer <4B, 65, 79>

Buffer decoding

transcode(Buffer.from([0x4b, 0x65, 0x79])).toHex()
// '4b6579'

Hexadecimal encoding

fromLatin1('Key').toHex()
// '4b6579'
transcode({
  encoding: 'latin1',
  text: 'Key',
}).toHex()
// '4b6579'

Hexadecimal decoding

fromHex('4b6579').toLatin1()
// 'Key'
transcode({
  encoding: 'hex',
  text: '4b6579',
}).toLatin1()
// 'Key'

JSON encoding

fromLatin1('Key').toJSON()
// '{"type":"Buffer","data":[75,101,121]}'
transcode({
  encoding: 'latin1',
  text: 'Key',
}).toJSON()
// '{"type":"Buffer","data":[75,101,121]}'

JSON decoding

fromJSON('{"type":"Buffer","data":[75,101,121]}').toLatin1()
// 'Key'
transcode({
  encoding: 'json',
  text: '{"type":"Buffer","data":[75,101,121]}',
}).toLatin1()
// 'Key'

Latin-1 encoding

transcode([0x4b, 0x65, 0x79]).toLatin1()
// 'Key'

Latin-1 decoding

fromLatin1('Key').toUInt8Array()
// Uint8Array <4B, 65, 79>
transcode({
  encoding: 'latin1',
  text: 'Key',
}).toUInt8Array()
// Uint8Array <4B, 65, 79>

number encoding

transcode([0x4b, 0x65, 0x79]).toNumber()
// 4941177

number decoding

transcode(4941177).toUInt8Array()
// Uint8Array <4B, 65, 79>

number[] encoding

fromLatin1('Key').toArray()
// [75, 101, 121]
transcode({
  encoding: 'latin1',
  text: 'Key',
}).toArray()
// [75, 101, 121]

number[] decoding

Byte

transcode([0b1001011, 0b1100101, 0b1111001]).toLatin1()
// 'Key'

Decimal

transcode([75, 101, 121]).toLatin1()
// 'Key'

Hexadecimal

transcode([0x4b, 0x65, 0x79]).toLatin1()
// 'Key'

Nibble

transcode([0b100_1011, 0b110_0101, 0b111_1001]).toLatin1()
// 'Key'

Octal

transcode([0o113, 0o145, 0o171]).toLatin1()
// 'Key'

Uint8Array encoding

fromLatin1('Key').toUInt8Array()
// Uint8Array <4B, 65, 79>
transcode({
  encoding: 'latin1',
  text: 'Key',
}).toUInt8Array()
// Uint8Array <4B, 65, 79>

Uint8Array decoding

transcode(Uint8Array.from([75, 101, 121])).toLatin1()
// 'Key'

UTF-8 encoding

transcode([0x4b, 0x65, 0x79]).toUTF8()
// 'Key'

UTF-16 LE encoding

transcode([0x4b, 0x65, 0x79]).toUTF16LE()
// '敋'

Interface & types

Function signatures provided here for reference. Built-in types are automatically usable in JavaScript. TypeScript is optional and not required.

transcode

type Transcode = (
  param:
    | Buffer
    | number
    | number[]
    | Uint8Array
    | {
        encoding: 'base64' | 'base64url' | 'binary' | 'hex' | 'json' | 'latin1'
        text: string
      }
) => {
  toArray(): number[]
  toBase64(): string
  toBase64URL(): string
  toBinary(): string
  toBuffer(): Buffer
  toHex(): string
  toJSON(): string
  toLatin1(): string
  toNumber(): number
  toUInt8Array(): Uint8Array
  toUTF8(): string
  toUTF16LE(): string
}

fromBase64, fromBase64URL, fromBinary, fromHex, fromJSON, fromLatin1

type FromString = (text: string) => {
  toArray(): number[]
  toBase64(): string
  toBase64URL(): string
  toBinary(): string
  toBuffer(): Buffer
  toHex(): string
  toJSON(): string
  toLatin1(): string
  toNumber(): number
  toUInt8Array(): Uint8Array
  toUTF8(): string
  toUTF16LE(): string
}