我正在处理最奇怪的问题。我有一个很大的字符串(大约500万字节),正试图缩小它。在缩小之前,我试图这样“重构”它:

  for(var i = 0; i < data.length; i++){
    var bb64 = "";
    var sb64 = "";

    //the big-ass string (~5 million bytes)
    bb64 = data[i];
    //initially one sixth of the big-ass string
    sb64 = bb64.substring(0, bb64.length/6);

    var start = bb64.length/6;
    var end = bb64.length;

    //for testing, re-add the remaining 5/6 characters
    for(var x = start; x < end; x++){
      sb64 += bb64[x]
    }

    //theoretically, these should be the same for each iteration of i
    console.log(bb64.length);
    console.log(sb64.length);

   /*actual results:
   i = 0:
   bb64:5458944
   sb64:5458944

   i = 1:
   bb64:5563292
   sb64:42651908

   i = 2:
   bb64:5555276
   sb64:42590452
   */
  }


然后,我用其他大型字符串(〜3百万个字节)对其进行了测试,结果是:

i = 0:
bb64:3132968
sb64:24019424

i = 1:
bb64:3635916
sb64:3635916

i = 2:
bb64:3791560
sb64:29068632


任何人都知道到底发生了什么..?

提前致谢!

最佳答案

“长度/ 6”除法不是四舍五入的,使用非四舍五入的值作为键会产生不可预测的结果。

关于javascript - Base 64字符串神奇地变大了。有时,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/37588143/

10-12 17:13