在 JavaScript 中将数字转换为基数 64 的最快方法?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6213227/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Fastest way to convert a number to radix 64 in JavaScript?
提问by callum
In JavaScript you can convert a number to a string representation with a specific radixas follows:
在 JavaScript 中,您可以将数字转换为具有特定基数的字符串表示形式,如下所示:
(12345).toString(36) // "9ix"
...and you can convert it back to a regular number like this:
...您可以将其转换回常规数字,如下所示:
parseInt("9ix", 36) // 12345
36 is the highest radix you can specify. It apparently uses the characters 0-9
and a-z
for the digits (36 total).
36 是您可以指定的最高基数。它显然使用字符0-9
和a-z
数字(总共 36 个)。
My question: what's the fastest way to convert a number to a base 64 representation (for example, using A-Z
, and -
and _
for the extra 28 digits)?
我的问题:将数字转换为 base 64 表示的最快方法是什么(例如,使用A-Z
, 和-
和_
额外的 28 位数字)?
Update: Four people have posted responses saying this question is duplicated, or that I'm looking for Base64. I'm not.
更新:四个人发布了回复说这个问题是重复的,或者我正在寻找 Base64。我不是。
"Base64" is a way of encoding binary data in a simple ASCII character set, to make it safe for transfer over networks etc. (so that text-only systems won't garble the binary).
“ Base64”是一种以简单的 ASCII 字符集对二进制数据进行编码的方法,以使其安全地通过网络等传输(以便纯文本系统不会混淆二进制)。
That's not what I'm asking about. I'm asking about converting numbersto a radix 64 string representation. (JavaScript's toString(radix)
does this automatically for any radix up to 36; I need a custom function to get radix 64.)
这不是我要问的。我问的是将数字转换为基数 64 的字符串表示形式。(JavaScripttoString(radix)
会自动为最多 36 的任何基数执行此操作;我需要一个自定义函数来获取基数 64。)
Update 2: Here are some input & output examples...
更新 2:以下是一些输入和输出示例...
0 → "0"
1 → "1"
9 → "9"
10 → "a"
35 → "z"
61 → "Z"
62 → "-"
63 → "_"
64 → "10"
65 → "11"
128 → "20"
etc.
采纳答案by Reb.Cabin
Here is a sketch for a solution for NUMBERS (not arrays of bytes :)
这是 NUMBERS 解决方案的草图(不是字节数组 :)
only for positive numbers, ignores fractional parts, and not really tested -- just a sketch!
仅适用于正数,忽略小数部分,并没有真正经过测试——只是一个草图!
Base64 = {
_Rixits :
// 0 8 16 24 32 40 48 56 63
// v v v v v v v v v
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+/",
// You have the freedom, here, to choose the glyphs you want for
// representing your base-64 numbers. The ASCII encoding guys usually
// choose a set of glyphs beginning with ABCD..., but, looking at
// your update #2, I deduce that you want glyphs beginning with
// 0123..., which is a fine choice and aligns the first ten numbers
// in base 64 with the first ten numbers in decimal.
// This cannot handle negative numbers and only works on the
// integer part, discarding the fractional part.
// Doing better means deciding on whether you're just representing
// the subset of javascript numbers of twos-complement 32-bit integers
// or going with base-64 representations for the bit pattern of the
// underlying IEEE floating-point number, or representing the mantissae
// and exponents separately, or some other possibility. For now, bail
fromNumber : function(number) {
if (isNaN(Number(number)) || number === null ||
number === Number.POSITIVE_INFINITY)
throw "The input is not valid";
if (number < 0)
throw "Can't represent negative numbers now";
var rixit; // like 'digit', only in some non-decimal radix
var residual = Math.floor(number);
var result = '';
while (true) {
rixit = residual % 64
// console.log("rixit : " + rixit);
// console.log("result before : " + result);
result = this._Rixits.charAt(rixit) + result;
// console.log("result after : " + result);
// console.log("residual before : " + residual);
residual = Math.floor(residual / 64);
// console.log("residual after : " + residual);
if (residual == 0)
break;
}
return result;
},
toNumber : function(rixits) {
var result = 0;
// console.log("rixits : " + rixits);
// console.log("rixits.split('') : " + rixits.split(''));
rixits = rixits.split('');
for (var e = 0; e < rixits.length; e++) {
// console.log("_Rixits.indexOf(" + rixits[e] + ") : " +
// this._Rixits.indexOf(rixits[e]));
// console.log("result before : " + result);
result = (result * 64) + this._Rixits.indexOf(rixits[e]);
// console.log("result after : " + result);
}
return result;
}
}
UPDATE: Here's some (very lightweight) testing of the above, for running in NodeJs where you have console.log.
更新:这是上面的一些(非常轻量级)测试,用于在具有 console.log 的 NodeJs 中运行。
function testBase64(x) {
console.log("My number is " + x);
var g = Base64.fromNumber(x);
console.log("My base-64 representation is " + g);
var h = Base64.toNumber(g);
console.log("Returning from base-64, I get " + h);
if (h !== Math.floor(x))
throw "TEST FAILED";
}
testBase64(0);
try {
testBase64(-1);
}
catch (err) {
console.log("caught >>>>>> " + err);
}
try {
testBase64(undefined);
}
catch (err) {
console.log("caught >>>>>> " + err);
}
try {
testBase64(null);
}
catch (err) {
console.log("caught >>>>>> " + err);
}
try {
testBase64(Number.NaN);
}
catch (err) {
console.log("caught >>>>>> " + err);
}
try {
testBase64(Number.POSITIVE_INFINITY);
}
catch (err) {
console.log("caught >>>>>> " + err);
}
try {
testBase64(Number.NEGATIVE_INFINITY);
}
catch (err) {
console.log("caught >>>>>> " + err);
}
for(i=0; i<100; i++)
testBase64(Math.random()*1e14);
回答by jahooma
Here's a version just for 32 bit ints, that is, any number between -2147483648 and 2147483647 (inclusive).
这是一个仅适用于 32 位整数的版本,即 -2147483648 和 2147483647(含)之间的任何数字。
I modified the version in the top answer by Reb Cabin. This should be quite a bit faster because it uses bit operations and lookup tables.
我在 Reb Cabin 的最佳答案中修改了版本。这应该会快一些,因为它使用位操作和查找表。
Base64 = (function () {
var digitsStr =
// 0 8 16 24 32 40 48 56 63
// v v v v v v v v v
"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz+-";
var digits = digitsStr.split('');
var digitsMap = {};
for (var i = 0; i < digits.length; i++) {
digitsMap[digits[i]] = i;
}
return {
fromInt: function(int32) {
var result = '';
while (true) {
result = digits[int32 & 0x3f] + result;
int32 >>>= 6;
if (int32 === 0)
break;
}
return result;
},
toInt: function(digitsStr) {
var result = 0;
var digits = digitsStr.split('');
for (var i = 0; i < digits.length; i++) {
result = (result << 6) + digitsMap[digits[i]];
}
return result;
}
};
})();
For example,
例如,
Base64.fromInt(-2147483648); // gives "200000"
Base64.toInt("200000"); // gives -2147483648
回答by monocle
Here's a different take
这是一个不同的看法
function base64(value) {
if (typeof(value) === 'number') {
return base64.getChars(value, '');
}
if (typeof(value) === 'string') {
if (value === '') { return NaN; }
return value.split('').reverse().reduce(function(prev, cur, i) {
return prev + base64.chars.indexOf(cur) * Math.pow(64, i);
}, 0);
}
}
base64.chars = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_";
base64.getChars = function(num, res) {
var mod = num % 64,
remaining = Math.floor(num / 64),
chars = base64.chars.charAt(mod) + res;
if (remaining <= 0) { return chars; }
return base64.getChars(remaining, chars);
};
回答by jam65st
I was looking for a solution to the same problem, but for ActionScript (AS3), and it was evident that many persons confuse Base64 encodewith 'numbers in base 64' (radix 64).
我正在寻找相同问题的解决方案,但对于ActionScript (AS3),很明显许多人将Base64 编码与'基数 64' (基数 64) 中的数字混淆。
The vast majority of sites offer solutions for 'computational cryptography'and not mathematics. As solutions, this are not useful for the conversion that we needed.
绝大多数站点提供“计算密码学”而不是数学的解决方案。作为解决方案,这对我们需要的转换没有用。
Prior to this consultation, and knew the methods toString (radix) and parseInt (radix), I was worked with Hex numbers (radix 16) in both colors, and other features.
在此咨询之前,并且知道 toString (radix) 和 parseInt (radix) 方法,我使用过两种颜色的十六进制数(基数 16)和其他功能。
However, neither in AS3?or JSthere exists a numerical method for transformations to and from radix 64.
但是,无论是在AS3还是JS 中,都不存在用于与基数 64 之间的转换的数值方法。
Before coming to this site I found:
在来这个网站之前,我发现:
- In various online calculators, radix 64not starting from scratch, but A.
Eg: convertix.com& alfredo4570.net - In radix 64is formed by the following sets of ordered characters: AZ, az, 0-9, + and / (these I have defined a constant: STR64)
- 在各种在线计算器中,基数 64不是从头开始,而是A。
例如:convertix.com& alfredo4570.net - 在基数 64中由以下有序字符集组成:AZ、az、0-9、+ 和 /(这些我定义了一个常量:STR64)
To avoid confusion with cryptographic methods, the methods to be used are based on well-known names:
为避免与加密方法混淆,要使用的方法基于众所周知的名称:
- toString / to64String
- parseInt / to64Parse
- toString / to64String
- parseInt / to64Parse
The code was be wrote in AS3, but is very clear (common with JS).
代码是用AS3编写的,但非常清晰(与 JS 相同)。
NOTE: Recommended use with number under: 1 * 1016
注意:建议使用以下数字:1 * 10 16
At the end, an example and results of operations are included.
最后附上实例和操作结果。
const STR64:Array = ('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/').split( '' );
// TRANSFORM NUMBERS BETWEEN radix 10 AND radix 64
/** Methods based on answers shared in:
* @url http://stackoverflow.com/questions/6213227/fastest-way-to-convert-a-number-to-radix-64-in-javascript
*/
// METHODS
/** to64String: Method to transform a radix 10 number to radix 64 number (as string)
* @param input Number for transform to radix 64 (as String)
* @param current String data (don't needed in request)
* @return String Number in radix 64 as String;
*
* @based http://stackoverflow.com/users/383780/monocle
* @based base64( Method for number to string - NOT string part )
*/
function to64String( input:Number, current:String = '' ):String
{
if ( input < 0 && current.length == 0 ){
input = input * - 1;
}
var modify:Number = input % 64;
var remain:Number = Math.floor( input / 64 );
var result:String = STR64[ modify ] + current;
return ( remain <= 0 ) ? result : to64String( remain, result );
}
/** to64Parse: Method for transform a number in radix 64 (as string) in radix 10 number
* @param input Number in radix 64 (as String) to transform in radix 10
* @return Number in radix 10
*
* @based http://stackoverflow.com/users/520997/reb-cabin
* @based Base64.toNumber( Method for string to number )
*/
function to64Parse ( input:String ):Number
{
var result:Number = 0;
var toProc:Array = input.split( '' );
var e:String;
for ( e in toProc ){
result = ( result * 64 ) + STR64.indexOf( toProc[ e ] );
}
return result;
}
// TEST
var i:int = 0;
var max:Number = 1000000000000;
var min:Number = 0;
for ( i == 0; i < 20; i++ ){
var num:Number = ( Math.ceil( Math.random() * ( max - min + 1 ) ) + min );
var s64:String = to64String( num );
var ret:Number = to64Parse ( s64 );
trace( i + '\t# ' + num + '\t' + s64 + '\t' + ret + '\t' + ( ret == num ) )
}
// TEST RESULT
/*
0 # 808936734685 LxYYv/d 808936734685 true
1 # 931332556532 NjXvwb0 931332556532 true
2 # 336368837395 E5RJSMT 336368837395 true
3 # 862123347820 Mi6jk9s 862123347820 true
4 # 174279278611 CiT2sAT 174279278611 true
5 # 279361353722 EELO/f6 279361353722 true
6 # 435602995568 GVr9jlw 435602995568 true
7 # 547163526063 H9lfNOv 547163526063 true
8 # 188017380425 CvGtYxJ 188017380425 true
9 # 720098771622 KepO0Km 720098771622 true
10 # 408089106903 F8EAZnX 408089106903 true
11 # 293941423763 ERwRi6T 293941423763 true
12 # 383302396164 Fk+mmkE 383302396164 true
13 # 695998940618 KIMxQXK 695998940618 true
14 # 584515331314 IgX1CTy 584515331314 true
15 # 528965753970 Hso0Nxy 528965753970 true
16 # 5324317143 E9WqHX 5324317143 true
17 # 772389841267 LPWBalz 772389841267 true
18 # 954212692102 N4rgjCG 954212692102 true
19 # 867031893694 MnfIMa+ 867031893694 true
*/
回答by kutuluk
Extremely fast implementation for all values of javascript safe integers range (from -9007199254740991
to 9007199254740991
):
javascript安全整数范围(从-9007199254740991
到9007199254740991
)的所有值的极快实现:
const alphabet = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
// binary to string lookup table
const b2s = alphabet.split('');
// string to binary lookup table
// 123 == 'z'.charCodeAt(0) + 1
const s2b = new Array(123);
for (let i = 0; i < alphabet.length; i++) {
s2b[alphabet.charCodeAt(i)] = i;
}
// number to base64
const ntob = (number) => {
if (number < 0) return `-${ntob(-number)}`;
let lo = number >>> 0;
let hi = (number / 4294967296) >>> 0;
let right = '';
while (hi > 0) {
right = b2s[0x3f & lo] + right;
lo >>>= 6;
lo |= (0x3f & hi) << 26;
hi >>>= 6;
}
let left = '';
do {
left = b2s[0x3f & lo] + left;
lo >>>= 6;
} while (lo > 0);
return left + right;
};
// base64 to number
const bton = (base64) => {
let number = 0;
const sign = base64.charAt(0) === '-' ? 1 : 0;
for (let i = sign; i < base64.length; i++) {
number = number * 64 + s2b[base64.charCodeAt(i)];
}
return sign ? -number : number;
};
npm: number-to-base64
npm:数字到 base64
Perfomance comparison: https://jsperf.com/number-to-base64-encoding
回答by Casey Flynn
I wrote an npm module for this type of operation, power-radix, that will help you. You can convert any number from any radix to any radix in a user-defined character encoding.
我为这种类型的操作编写了一个 npm 模块power-radix,它将对您有所帮助。您可以将任何数字从任何基数转换为用户定义字符编码中的任何基数。
For example:
例如:
var base = ['Q', 'W', 'E', 'R', 'T', 'Y', 'I', 'O', 'U'];
new PowerRadix([1, 0], 10).toArray(base); // ['W', 'Q']
new PowerRadix('10', 10).toArray(base); // ['W', 'Q']
new PowerRadix(10, 10).toArray(base); // ['W', 'Q']
new PowerRadix([1, 0], 10).toString(base); // "WQ"
new PowerRadix('10', 10).toString(base); // "WQ"
new PowerRadix(10, 10).toString(base); // "WQ"
The module also supports custom source radix encodings.
该模块还支持自定义源基数编码。
new PowerRadix('ba', ['a', 'b']); // base 2 source radix, uses 'a' = 0 & 'b' = 1 character set.
new PowerRadix('ba', ['a', 'b']).toString(10); // returns "2"
回答by le_m
The following implementation converts positive, negative and non-integer numbers to an arbitrary base. The conversion back to decimal is easily implemented in a similar fashion:
以下实现将正数、负数和非整数转换为任意基数。转换回十进制很容易以类似的方式实现:
function toAnyBase(num, base) {
if (!Number.isInteger(base) || base < 2) throw new RangeError("toAnyBase() base argument must be an integer >= 2");
if (!Number.isFinite(num)) return num.toString();
if (num < 0) return "-" + toAnyBase(-num, base);
const digits = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ#_",
inv_base = 1 / base;
var result = "",
residual;
// Integer part:
residual = Math.trunc(num);
do {
result = digits.charAt(residual % base) + result;
residual = Math.trunc(residual * inv_base);
} while (residual != 0);
// Fractional part:
residual = num % 1;
if (residual != 0) {
result += ".";
var max = 1000;
do {
residual *= base;
result += digits.charAt(Math.trunc(residual));
residual %= 1;
} while (residual != 0 && --max != 0);
}
return result;
}
console.log(toAnyBase( 64, 64)); // "10"
console.log(toAnyBase(-1.5, 64)); // "-1.w"
回答by Branya The Great
If you are using NodeJS, you can use the following code:
如果您使用的是 NodeJS,则可以使用以下代码:
var base64 = Buffer.from([i>>24,i>>16,i>>8,i]).toString('base64').substr(0,6);
回答by Femi
Well, you could just use any Javascript Base64 library: perhaps this questionanswers it?
好吧,您可以使用任何 Javascript Base64 库:也许这个问题可以回答它?
EDIT: Binary data is essentially just a sequence of bytes. If you assume the bytes represent a single number you can represent the sequence of bytes as a base 64 string. Decode them and do some trivial math on the bytes to get a number. Convert the number to a sequence of bytes and encode to get a string. Seems quite reasonable, unless you are somehow invested in the specific characters used in the String.
编辑:二进制数据本质上只是一个字节序列。如果假设字节表示单个数字,则可以将字节序列表示为 base 64 字符串。解码它们并对字节进行一些简单的数学运算以得到一个数字。将数字转换为字节序列并编码以获取字符串。似乎很合理,除非您以某种方式投资于 String 中使用的特定字符。
回答by Calculon000
I was after the same solution and I think I've generalized what this person is after with as few lines as possible in basic Javascript. Should work for any positive integer, and endex can be whatever length you want for whatever base you want as long as all the characters are unique.
我追求的是相同的解决方案,我想我已经用基本的 Javascript 中尽可能少的行概括了这个人所追求的东西。应该适用于任何正整数,并且只要所有字符都是唯一的,endex 可以是您想要的任何长度。
var endex = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz-_";
function encode(intcode){
if(intcode < endex.length){
return endex[intcode];
}else{
return encode(Math.floor(intcode/endex.length)) + endex[intcode%endex.length];
}
}
function decode(charcode){
if(charcode.length < 2){
return endex.indexOf(charcode);
}else{
return (decode(charcode.slice(0, -1)) * endex.length) + endex.indexOf(charcode.slice(-1));
}
}