基数62
基础62 编码/解码器
安装
npm install --save @fry/base62
使用量
encode() 预期可以转换成 Buffer (例如a) string, (中文(简体) ),
a 一个 Array, (中文(简体) ), ArrayBuffer,或一个 Buffer) 它返回 Base62 编码
绳子.
decode() 返回在 a 中解码的数据 Buffer 它有很好的阅读器
二进制数据以及快捷键 toString() 可以返回编码后的数据
内 utf8 (违约), base64, (中文(简体) ), hex等 (中文(简体) ).
二进制输入
const base62 = require('@fry/base62');
const encoded = base62.encode([0x2a, 0x2a]);
// encoded => AYA
const decoded = base62.decode(encoded);
// decoded => <Buffer 2a 2a>
decoded[0]; // => 0x2a
decoded.readUInt16BE(); // => 0x2a2a
字符串输入
const encoded = base62.encode('Memento vivere');
// encoded => JMLjPMvqRo1sQNPbSc5
const decoded = base62.decode(encoded);
// decoded => <Buffer 4d 65 6d 65 6e 74 6f 20 76 69 76 65 72 65>
decoded.toString(); // => Memento vivere
自定义符号表
Base62 编码没有标准,所以各种编码器可能使用不同的编码
符号表。 此模块默认会在
他们的自然秩序: 0-9A-Za-z。 。 。 .
要使用另一组字符首先启动它们 prepareSymbolTable()
,然后将生成的对象作为第二个参数提供给编码/解码
函数,例如:
const symbols = base62.prepareSymbolTable('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789');
const encoded = base62.encode([0x53, 0xfe, 0x92], symbols);
// encoded => U98kC
const decoded = base62.decode(encoded, symbols);
// decoded => <Buffer 53 fe 92>
Base62 编码程序
Base62符号表限定为62个字符,只是6个比特的害羞 信息通报。 这意味着需要特别注意编码 任何给定的字节流。 此模块采用读取sextet的方法 (6- 位块) 从二进制流将其编码为符号表, 持续时间更长 因为块不会溢出。 如果六分位符位于“流出”中 然后在编码特定块时只使用 5 比特 以符号表最后两个字符之一的存在来输出. 溢出位留在溪流中,直到下一个sextet被读取. 这个 编码过程中,最后一个块也可以被以零为单位的左加法 解码器将检测和倒置,以确保底部的字节对齐 二进制流.
编码愉快!
/ 弗莱