发布一个Nodejs module - 数字与字符串转换模块baseN,支持2~62间任意进制转换,并可自定义字符base

发布一个Nodejs module - 数字与字符串转换模块baseN,支持2~62间任意进制转换,并可自定义字符base

github地址 baseN

baseN: 可以将2的53次方以内的任意整数(包括负数)转化为字符串以及还原回该整数,可以自行配置进制和使用的字符。

安装

$ npm install basen

使用

详细使用可参见测试文件 baseN.test.js

new BaseN()

三种方式新建baseN对象

var baseN = new BaseN();//使用默认的base: 52个英文字母和10个阿拉伯数字
var baseN = new BaseN(15);//设置为15进制,取’默认base‘的前15个char作为新的base
var baseN = new BaseN({
    base:[
        'a','b','c','d','e','0','1','2'
    ]
});//自定义base,这个例子是8进制

encode()

数字转为字符串

var testCase = [
    [0, '0'],
    [1, '1'],
    [20, 'k'],
    [1000, 'g8'],
    [20130718, '1msV0']
];
var baseN = new BaseN();

testCase.forEach(function(item) { baseN.encode(item[0]); });

decode()

字符串还原数字

var testCase = [
    [0, '0'],
    [1, '1'],
    [20, 'k'],
    [1000, 'g8'],
    [20130718, '1msV0']
];
var baseN = new BaseN();

testCase.forEach(function(item) { baseN.decode(item[1]); });

reBase()

重置base

var baseN = new BaseN();
baseN.reBase(10);//随机选取’默认base‘里的10个char作为新base
baseN.base.length == 10;//true

var testCase = [0, 1, 20, 1000, 1024, 123456789]; testCase.forEach(function(item) { baseN.decode(baseN.encode(item))==item;//true });


4 回复

发布一个Nodejs module - 数字与字符串转换模块baseN,支持2~62间任意进制转换,并可自定义字符base

GitHub 地址

baseN

baseN

baseN 是一个 Node.js 模块,它可以将 2 的 53 次方以内的任意整数(包括负数)转换为字符串,也可以将字符串还原成该整数。用户可以根据需要自定义进制和使用的字符。

安装

$ npm install basen

使用

详细使用方法可以参考测试文件 baseN.test.js

new BaseN()

有三种方式来创建 BaseN 对象:

// 使用默认的 base: 52 个英文字母和 10 个阿拉伯数字
var baseN = new BaseN();

// 设置为 15 进制,取默认 base 的前 15 个字符作为新的 base
var baseN = new BaseN(15);

// 自定义 base,例如这是一个 8 进制
var baseN = new BaseN({
    base: ['a', 'b', 'c', 'd', 'e', '0', '1', '2']
});
encode()

将数字转换为字符串:

var testCase = [
    [0, '0'],
    [1, '1'],
    [20, 'k'],
    [1000, 'g8'],
    [20130718, '1msV0']
];

var baseN = new BaseN();

testCase.forEach(function(item) {
    console.log(baseN.encode(item[0])); // 输出对应的字符串
});
decode()

将字符串还原为数字:

var testCase = [
    [0, '0'],
    [1, '1'],
    [20, 'k'],
    [1000, 'g8'],
    [20130718, '1msV0']
];

var baseN = new BaseN();

testCase.forEach(function(item) {
    console.log(baseN.decode(item[1])); // 输出对应的数字
});
reBase()

重新设置 base:

var baseN = new BaseN();
baseN.reBase(10); // 随机选取默认 base 中的 10 个字符作为新 base

console.log(baseN.base.length === 10); // 输出 true

var testCase = [0, 1, 20, 1000, 1024, 123456789];
testCase.forEach(function(item) {
    console.log(baseN.decode(baseN.encode(item)) === item); // 输出 true
});

通过这些方法,您可以灵活地进行不同进制之间的转换,并且能够自定义字符集。希望这个模块能对您的项目有所帮助!


用这个做加密应该有点意思
有过没明白为是什么范围是2的53次方 比较了toString和parseInt的效率,加油把效率搞上去哦

var numb = 1212121312423423,n=100000,str="bxnsudde67";
console.log("10转36比较")
var t1 = new Date();
for(var i=0;i<n;i++){
    b.encode(numb)
}
console.log(new Date() - t1)
var t2 = new Date();
for(var i=0;i<n;i++){
    numb.toString(36)
}
console.log(new Date() - t2)
console.log("36转10比较")
var t3 = new Date();
for(var i=0;i<n;i++){
    b.decode(str)
}
console.log(new Date() - t3)
var t4 = new Date();
for(var i=0;i<n;i++){
    parseInt(str,36)
}
console.log(new Date() - t4)

结果:

10转36比较
560
159
36转10比较
556
164

设置2的53次方,是为了避免查过javascript所能表示的整数的最大值。这个不适合做加密,做字符串混淆或是整数缩短倒是可以。

发布一个Nodejs module - 数字与字符串转换模块baseN

GitHub地址

baseN

模块概述

baseN: 可以将2的53次方以内的任意整数(包括负数)转化为字符串以及还原回该整数。支持2到62之间的任意进制转换,并且可以自定义字符集。

安装

$ npm install basen

使用方法

详细的使用方法可以参考测试文件 baseN.test.js

创建 baseN 对象

有三种方式来创建 baseN 对象:

var baseN = new BaseN(); // 使用默认的base:52个英文字母和10个阿拉伯数字
var baseN = new BaseN(15); // 设置为15进制,取默认base的前15个字符作为新的base
var baseN = new BaseN({
    base: ['a', 'b', 'c', 'd', 'e', '0', '1', '2']
}); // 自定义base,这个例子是8进制
encode()

将数字转换为字符串:

var testCase = [
    [0, '0'],
    [1, '1'],
    [20, 'k'],
    [1000, 'g8'],
    [20130718, '1msV0']
];
var baseN = new BaseN();

testCase.forEach(function(item) {
    console.log(baseN.encode(item[0])); // 输出对应的字符串
});
decode()

将字符串还原为数字:

var testCase = [
    [0, '0'],
    [1, '1'],
    [20, 'k'],
    [1000, 'g8'],
    [20130718, '1msV0']
];
var baseN = new BaseN();

testCase.forEach(function(item) {
    console.log(baseN.decode(item[1])); // 输出对应的数字
});
reBase()

重新设置 base 字符集:

var baseN = new BaseN();
baseN.reBase(10); // 随机选取默认base里的10个字符作为新base
console.log(baseN.base.length === 10); // true

var testCase = [0, 1, 20, 1000, 1024, 123456789];
testCase.forEach(function(item) {
    console.log(baseN.decode(baseN.encode(item)) === item); // true
});

以上是模块的基本用法和示例代码。希望对你有所帮助!

回到顶部