Convert binary data between any two formats and encodings listed here.
Further information: Uint8Array
(MDN)
number[]
Uint8Array
Further information: node:buffer
(Node.js)
Buffer
(browser-compatible, Node.js not required)
Further information: Number
(MDN)
number
Further information: Binary number (Wikipedia) | JSON
(MDN) | node:buffer
character encodings (Node.js) | Unicode (Wikipedia)
'base64'
'base64url'
'binary'
(binary numberstring
, not the legacy Node.js alias of the same name for'latin1'
encoding)'hex'
'json'
'latin1'
'utf8'
(encode output only)'utf16le'
(encode output only)
Instant demonstration: @stassi/binary-transcoder
(RunKit + npm)
npm i @stassi/binary-transcoder
import {
fromBase64,
fromBase64URL,
fromBinary,
fromHex,
fromJSON,
fromLatin1,
transcode,
} from '@stassi/binary-transcoder'
const {
fromBase64,
fromBase64URL,
fromBinary,
fromHex,
fromJSON,
fromLatin1,
transcode,
} = require('@stassi/binary-transcoder')
import {
fromBase64,
fromBase64URL,
fromBinary,
fromHex,
fromJSON,
fromLatin1,
transcode,
} from 'https://cdn.skypack.dev/@stassi/binary-transcoder'
transcode([0x3e, 0x3f, 0xfe, 0xff]).toBase64()
// 'Pj/+/w=='
fromBase64('Pj/+/w==').toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>
transcode({
encoding: 'base64',
text: 'Pj/+/w==',
}).toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>
transcode([0x3e, 0x3f, 0xfe, 0xff]).toBase64URL()
// 'Pj_-_w'
fromBase64URL('Pj_-_w').toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>
transcode({
encoding: 'base64url',
text: 'Pj_-_w',
}).toUInt8Array()
// Uint8Array <3E, 3F, FE, FF>
transcode([0b1001011, 0b1100101, 0b1111001]).toBinary()
// '010010110110010101111001'
fromBinary('010010110110010101111001').toUInt8Array()
// Uint8Array <4B, 65, 79>
transcode({
encoding: 'binary',
text: '010010110110010101111001',
}).toUInt8Array()
// Uint8Array <4B, 65, 79>
fromHex('4b6579').toBuffer()
// Buffer <4B, 65, 79>
transcode({
encoding: 'hex',
text: '4b6579',
}).toBuffer()
// Buffer <4B, 65, 79>
transcode(Buffer.from([0x4b, 0x65, 0x79])).toHex()
// '4b6579'
fromLatin1('Key').toHex()
// '4b6579'
transcode({
encoding: 'latin1',
text: 'Key',
}).toHex()
// '4b6579'
fromHex('4b6579').toLatin1()
// 'Key'
transcode({
encoding: 'hex',
text: '4b6579',
}).toLatin1()
// 'Key'
fromLatin1('Key').toJSON()
// '{"type":"Buffer","data":[75,101,121]}'
transcode({
encoding: 'latin1',
text: 'Key',
}).toJSON()
// '{"type":"Buffer","data":[75,101,121]}'
fromJSON('{"type":"Buffer","data":[75,101,121]}').toLatin1()
// 'Key'
transcode({
encoding: 'json',
text: '{"type":"Buffer","data":[75,101,121]}',
}).toLatin1()
// 'Key'
transcode([0x4b, 0x65, 0x79]).toLatin1()
// 'Key'
fromLatin1('Key').toUInt8Array()
// Uint8Array <4B, 65, 79>
transcode({
encoding: 'latin1',
text: 'Key',
}).toUInt8Array()
// Uint8Array <4B, 65, 79>
transcode([0x4b, 0x65, 0x79]).toNumber()
// 4941177
transcode(4941177).toUInt8Array()
// Uint8Array <4B, 65, 79>
fromLatin1('Key').toArray()
// [75, 101, 121]
transcode({
encoding: 'latin1',
text: 'Key',
}).toArray()
// [75, 101, 121]
transcode([0b1001011, 0b1100101, 0b1111001]).toLatin1()
// 'Key'
transcode([75, 101, 121]).toLatin1()
// 'Key'
transcode([0x4b, 0x65, 0x79]).toLatin1()
// 'Key'
transcode([0b100_1011, 0b110_0101, 0b111_1001]).toLatin1()
// 'Key'
transcode([0o113, 0o145, 0o171]).toLatin1()
// 'Key'
fromLatin1('Key').toUInt8Array()
// Uint8Array <4B, 65, 79>
transcode({
encoding: 'latin1',
text: 'Key',
}).toUInt8Array()
// Uint8Array <4B, 65, 79>
transcode(Uint8Array.from([75, 101, 121])).toLatin1()
// 'Key'
transcode([0x4b, 0x65, 0x79]).toUTF8()
// 'Key'
transcode([0x4b, 0x65, 0x79]).toUTF16LE()
// '敋'
Function signatures provided here for reference. Built-in types are automatically usable in JavaScript. TypeScript is optional and not required.
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
}
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
}