@47ng/codec
浏览器和节点.js中Uint8Array从/入UTF-8,base64url和六进制的通用转换
特征
可用的解码器 :
utf8- UTF-8号机车b64- 基础64 URL(RFC-4648,第5节(中文(简体) )hex- 小写十六进制- 字符串表达式之间的简单转换
- 编码检测
当心!
utf8使用不同于其他编码器的常规(编码和解码) ),以反映如何进行TextEncoder财务报告和审定财务报表TextDecoder行为.
安装
$ yarn add @47ng/codec
# or
$ npm i @47ng/codec
支助
- 节点.js: >=11.x
- 浏览器: 见 动画 页:1
TextEncoder编号 :TextDecoder
实例
import { b64, hex, utf8 } from '@47ng/codec'
b64.encode(utf8.encode('Hello, World !')) // SGVsbG8sIFdvcmxkICE=
hex.encode(b64.decode('SGVsbG8sIFdvcmxkICE=')) // 48656c6c6f2c20576f726c642021
utf8.decode(hex.decode('48656c6c6f2c20576f726c642021')) // Hello, World !
b64.decode('SGVsbG8sIFdvcmxkICE=') // <Buffer 48 65 6c 6c 6f 2c 20 57 6f 72 6c 64 20 21>
文档
UTF-8 导弹
utf8.encode: 将 UTF-8 字符串转换成字节数组( Uint8Array)utf8.decode: 将数组字节转换为 UTF-8 字符串
实例:
import { utf8 } from '@47ng/codec'
const uint8Array = utf8.encode('Hello, World!')
// Uint8Array [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
const backToText = utf8.decode(uint8Array)
// 'Hello, World!'
基地64
b64.encode: 将字节数组转换为 Base64url 字符串b64.decode: 将基数64 字符串转换成字节数组b64.urlSafe: 将标准碱基64字符串转换为碱基64urlb64.urlUnsafe: 将 Base64url 字符串转换为 Base64 词典
说明:用于解码,支持任何字典,并跟踪编码(
=)是可选的.
实例:
import { b64, utf8 } from '@47ng/codec'
const uint8Array = b64.decode('8J-Ri_CfjI0')
// Uint8Array [240, 159, 145, 139, 240, 159, 140, 141]
// Encoding always emits padding
const backToBase64 = b64.encode(uint8Array)
// '8J-Ri_CfjI0='
const asText = utf8.decode(uint8Array)
// '👋🌍'
十六进制
hex.encode: 将数组字节转换为十六进制字符串hex.decode: 将十六进制字符串转换成字节数组
说明:解码接受任何大小写(小写,大写,混合).
实例:
import { hex } from '@47ng/codec'
const uint8Array = hex.decode('48656C6C6F2C20576f726c642021')
// Uint8Array [72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]
// Encoding is always lowercase
const backToBase64 = hex.encode(uint8Array)
// '48656c6c6f2c20576f726c642021'
公用事业
库输出从一个字符串转换的方便方法 向另一个代表:
import {
hexToBase64url,
base64ToHex,
utf8ToBase64,
base64toUTF8,
utf8ToHex,
hexToUTF8
} from '@47ng/codec'
base64ToHex('SGVsbG8sIFdvcmxkICE=')
// 48656c6c6f2c20576f726c642021
hexToBase64url('48656c6c6f2c20576f726c642021')
// SGVsbG8sIFdvcmxkICE=
utf8ToBase64('Hello, World !')
// SGVsbG8sIFdvcmxkICE=
base64toUTF8('SGVsbG8sIFdvcmxkICE=')
// Hello, World !
utf8ToHex('Hello, World !')
// 48656c6c6f2c20576f726c642021
hexToUTF8('48656c6c6f2c20576f726c642021')
// Hello, World !
它还会导出具有强烈类型Script的编码器/解码器对象,以帮助您 建立自己的编码器和解码器 :
import { encoders, decoders, Encoding } from '@47ng/codec'
// type Encoding = 'base64' | 'utf8' | 'hex'
function convert(
input: string,
inputEncoding: Encoding,
outputEncoding: Encoding
): string {
const decoder = decoders[inputEncoding]
const encoder = encoders[outputEncoding]
return encoder(decoder(input))
}
convert('Hello, World!', 'utf8', 'base64')
// SGVsbG8sIFdvcmxkICE
convert('Hello, World!', 'utf8', 'hex')
// 48656c6c6f2c20576f726c642021
您可以检测字符串的编码 :
import { detectEncoding } from '@47ng/codec'
detectEncoding('baadf00dcafebabe') // hex
// Both variants of base64 are detected
detectEncoding('SGVs+G8s/FdvcmxkICE=') // base64
detectEncoding('SGVs-G8s_FdvcmxkICE=') // base64
detectEncoding('not hex not base64') // utf8
许可证
麻省理工学院 - 由 \ \ \ 由 \ \ \ 由 \ 弗朗索瓦·贝斯。 。 。 .
工作时用这个包? 赞助我 帮助支持和维护.