将数字转换为尽可能短的字符串,同时保留唯一性

时间:2010-04-01 00:52:58

标签: javascript

我有一个数字,比如“123456”,我需要将它映射到字符串,任何字符串。地图函数的唯一约束是:

  • 每个数字必须映射到唯一的字符串(这意味着字符串可以任意长)
  • 字符串只能包含0-9,a-z,A-Z

什么地图函数会产生最短的字符串?

首选JavaScript解决方案。

注意:显然,最简单的解决方案是使用原始数字,因此请确保您的解决方案比这更好。

2 个答案:

答案 0 :(得分:32)

您可能需要使用 Base 36 Base 62

Base 36对于不区分大小写的字母数字字符来说是最紧凑的,但如果你想利用区分大小写,Base 62的紧凑性会大约增加20%。

对于Base 36,您可以轻松使用JavaScript的Number.toString(radix)方法,如下所示:

var n = 123456;
n.toString(36); // returns: "2n9c"

对于Base 62,您可能需要检查this forum post。基本上你应该能够做到以下几点:

Number.prototype.toBase = function (base) {
    var symbols = 
    "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ".split("");
    var decimal = this;
    var conversion = "";

    if (base > symbols.length || base <= 1) {
        return false;
    }

    while (decimal >= 1) {
        conversion = symbols[(decimal - (base * Math.floor(decimal / base)))] + 
                     conversion;
        decimal = Math.floor(decimal / base);
    }

    return (base < 11) ? parseInt(conversion) : conversion;
}

var n = 123456;
n.toBase(62); // returns: "w7e"

答案 1 :(得分:0)

我这样做是为了创建类似YouTube的密钥。它使用正则表达式捕获数字,然后在匹配列表上循环以组合字符串。

chars数组的顺序是任意的,你可以实际做任何你喜欢的东西,但是一旦你把它设置到位,以后不要改变它,否则你可能遇到重复的键,并确保你没有任何重复。

这里,num是保存输入键的变量。在这个样本中它很长,但它可以是任何长度。

var chars = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"]
num = "000102030405060708091011121314151617181920212223242526272829303132333435373839404142434445464748495051525354555657585960616263646566";
getnums = num.match(/(0|6[0-1]|[0-5]?[0-9])/g);
to62 = "";
for (var i=0;i<getnums.length;i++) {
    to62 = to62 + chars[parseInt(getnums[i])];
    // console.log(getnums[i] + ": " + chars[parseInt(getnums[i])])
}
console.log(to62);

这里是Cold Fusion中的一个实现,它使用基于1的数组,而不是javascript的基于0的数组,以防它对任何人都有帮助。

<cfscript>
  chars = ["0","1","2","3","4","5","6","7","8","9","a","b","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"];
  num = "0001020304050607080910111213141516171819202122232425262728293031323334353738394041424344454614748495051525354555657585960616263646566";
  getnums = ReMatch("(0|6[0-1]|[0-5]?[0-9])",num);
  to62 = "";
  for (i=1; i<=arraylen(getnums); i=i+1) {
      to62 = to62 & chars[getnums[i]+1];
  }
  writeoutput(to62);
</cfscript>