【问题标题】:Convert 170 Digit Binary String Into Shorter Format and Back Again (Javascript)将 170 位二进制字符串转换为更短的格式并再次返回 (Javascript)
【发布时间】:2015-07-13 07:41:51
【问题描述】:

我做了一个小谜题模拟here

170 个单元的开/关状态(有些对用户不可见)存储在一个数组中,为了能够重新创建特定配置,我在页面底部显示了数组的内容,然后可以作为 URL 参数放入,以在页面加载时“设置”特定配置like so.

我的问题是数组的输出是一个170位的二进制数,这样比较麻烦!

我尝试过使用

parseInt(input,2).toString(30)

parseInt(input,30).toString(2)

作为一种简单地将这个 170 位二进制数转换为更简洁的字母数字格式的方法(然后再次返回以由我的“设置”初始化程序读取),但据我所知,我正在处理的数字是太大了,不适合那种功能。

我的下一个想法是我可以将 170 位数字拆分为函数可以消化的部分,但是当我确定这种转换一定很常见时,这似乎有点像重新发明轮子并且有人可以让我了解“正确”的解决方法。

提前致谢!

【问题讨论】:

  • 您需要将其分解为更小的组件。这是因为1111...1111 = 1.4965776766268446e+51 大于 Number.MAX_SAFE_INTEGER。

标签: javascript binary type-conversion url-parameters


【解决方案1】:

你的想法是正确的,只是 JavaScript 不能准确地表示那么大的数字。当您使用 parseInt 将其转换为 JavaScript 数字时,您的 170 位数字会失去其准确性;并不能逐位表示原来的数字。

解决方案很简单:滚动你自己的数字解析函数,将 170 位数字分成更小的块。

function encode(a) {
    var b = "";
    while (a.length > 0) {
        b = parseInt(a.slice(-5), 2).toString(32) + b;
        a = a.slice(0, -5);
    }
    return b;
}

function decode(a) {
    var b = "";
    while (a.length > 0) {
        b = ("00000" + parseInt(a.slice(-1), 32).toString(2)).slice(-5) + b;
        a = a.slice(0, -1);
    }
    return b;
}

var s = "00000000000000010101110100001010100010000111011101000010101000100001010111011100000000000000010001110010001000101001000100010100100010001010000001110111001000000000000000";
var e = encode(s); // "000lq2k8et1a45es002748kh2i4a0tp000"
var d = decode(e); // d === s

更通用的功能:

function convert(string, base1, base2) {
    var result = "",
        chunkw = 0,  // number of characters to write per chunk
        chunkr = 0,  // number of characters to read per chunk
        padstr = "", // string of zeros for padding the write chunks
        slice;
    while (Math.pow(2, chunkw) < base1) chunkw += 1;
    while (Math.pow(2, chunkr) < base2) chunkr += 1;
    while (padstr.length < chunkw) padstr += "0";
    while (string.length > 0) {
        slice = string.slice(-chunkr);
        slice = parseInt(slice, base1).toString(base2);
        slice = (padstr + slice).slice(-chunkw);
        result = slice + result;
        string = string.slice(0, -chunkr);
    }
    return result;
}
var x = "00000000000000010101110100001010100010000111011101000010101000100001010111011100000000000000010001110010001000101001000100010100100010001010000001110111001000000000000000";
var a = convert(x, 2, 32);
var b = convert(a, 32, 2);
console.log(x + "\n" + a + "\n" + b);
//       00000000000000010101110100001010100010000111011101000010101000100001010111011100000000000000010001110010001000101001000100010100100010001010000001110111001000000000000000
//       000lq2k8et1a45es002748kh2i4a0tp000
//       00000000000000010101110100001010100010000111011101000010101000100001010111011100000000000000010001110010001000101001000100010100100010001010000001110111001000000000000000

【讨论】:

猜你喜欢
  • 2012-02-05
  • 2017-12-18
  • 1970-01-01
  • 2023-03-02
  • 1970-01-01
  • 2019-02-02
  • 2015-01-19
  • 2013-02-18
  • 1970-01-01
相关资源
最近更新 更多