json 到 base64 转换
json to base64 conversion
我一直在寻找 pako 将我的 base64 更改为 JSON,这让我可以完美地工作。但我该如何扭转呢?
我使用了在线JSON转base64,但结果与我最初输入的原始base64不同。
我在这个代码段的评论中留下了我的 base64 示例及其输出(也有评论)。我可以使用 pako 将其恢复到原始状态吗?
//eNqrVsoozcnxdFGyMjQwtjQwN9dRSq0oSC0qcSrNzElRskpLzClO1VEqT00syM8rVrKKNjUwMInVUSouSE3OBMpBRCxBItmZYAWGBgZmIINiawE68xw1
//{"hullID":1039077,"expertBuild":false,"weapons":[5004],"specials":[5009],"skins":[10069077]}
submit.onclick = function(){
// Get some base64
var b64Data = document.getElementById("KixCode").value;;
// Decode base64 (convert ascii to binary)
var strData = atob(b64Data);
// Convert binary string to character-number array
var charData = strData.split('').map(function(x){return x.charCodeAt(0);});
// Turn number array into byte-array
var binData = new Uint8Array(charData);
// Pako magic
var data = pako.inflate(binData);
// Convert gunzipped byteArray back to ascii string:
var strData = String.fromCharCode.apply(null, new Uint16Array(data));
// Output
var KixCode = strData
document.getElementById("output").innerText = KixCode;
}
<div>
<label for="KixCode">Kix Code: </label><input id="KixCode" type="text" value="eNqrVsoozcnxdFGyMjQwtjQwN9dRSq0oSC0qcSrNzElRskpLzClO1VEqT00syM8rVrKKNjUwMInVUSouSE3OBMpBRCxBItmZYAWGBgZmIINiawE68xw1"/><br /><br />
<button id="submit">Submit</button>
</div>
<p>
<span id="output"></span>
</p>
<script src="https://rawgit.com/nodeca/pako/master/dist/pako.js"></script>
可以使用pako的deflate
方法。问题是编码数据有一些 header 信息,这些信息提供了有关如何执行压缩的信息。该信息不包含在解压结果中(inflate
)。
你的示例数据原来是用 7 级压缩的,但默认为 deflate
method is level 6. This results in a difference in the second byte that is returned by deflate
when compared to the second byte of the data that was passed to inflate
. This second byte is part of the header。
注意:我不是很了解这一切——我只是提供以上内容供您进一步分析。重要的是选项会影响压缩的执行方式。
对于您的示例,将选项 { level: 7 }
传递给 deflate
方法将使结果与您最初将其传递给 inflate
的结果相同:
submit.onclick = function(){
var b64Data = document.getElementById("KixCode").value;
var strData = atob(b64Data);
var charData = Array.from(strData, x => x.charCodeAt(0));
var binData = new Uint8Array(charData);
var data = pako.inflate(binData);
var KixCode = String.fromCharCode.apply(null, new Uint16Array(data));
document.getElementById("output").innerText = KixCode;
// Reverse operation
var data2 = Array.from(KixCode, c => c.charCodeAt());
var binData2 = pako.deflate(data2, { level: 7 }); // <-- option
var charData2 = [...binData2];
var strData2 = charData2.map(i => String.fromCharCode(i)).join("");
var b64Data2 = btoa(strData2);
document.getElementById("verif").innerText = b64Data2 === b64Data ? "Reversal is correct" : "Reversal is not the same";
}
<div><label for="KixCode">Kix Code: </label><input id="KixCode" type="text" style="width: 100%" value="eNqrVsoozcnxdFGyMjQwtjQwN9dRSq0oSC0qcSrNzElRskpLzClO1VEqT00syM8rVrKKNjUwMInVUSouSE3OBMpBRCxBItmZYAWGBgZmIINiawE68xw1"></div>
<div><button id="submit">Submit</button></div>
<div id="output"></div>
<div id="verif"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pako/1.0.11/pako.min.js" integrity="sha512-euWc/Qv8Kp0CbTX1M+Q3BvUyoOaq9Au50TT7vz3MFf5ver39ybq6zV+RngDY8eN8AIQFigxjwYv6jhoP546vfQ==" crossorigin="anonymous"></script>
我一直在寻找 pako 将我的 base64 更改为 JSON,这让我可以完美地工作。但我该如何扭转呢?
我使用了在线JSON转base64,但结果与我最初输入的原始base64不同。
我在这个代码段的评论中留下了我的 base64 示例及其输出(也有评论)。我可以使用 pako 将其恢复到原始状态吗?
//eNqrVsoozcnxdFGyMjQwtjQwN9dRSq0oSC0qcSrNzElRskpLzClO1VEqT00syM8rVrKKNjUwMInVUSouSE3OBMpBRCxBItmZYAWGBgZmIINiawE68xw1
//{"hullID":1039077,"expertBuild":false,"weapons":[5004],"specials":[5009],"skins":[10069077]}
submit.onclick = function(){
// Get some base64
var b64Data = document.getElementById("KixCode").value;;
// Decode base64 (convert ascii to binary)
var strData = atob(b64Data);
// Convert binary string to character-number array
var charData = strData.split('').map(function(x){return x.charCodeAt(0);});
// Turn number array into byte-array
var binData = new Uint8Array(charData);
// Pako magic
var data = pako.inflate(binData);
// Convert gunzipped byteArray back to ascii string:
var strData = String.fromCharCode.apply(null, new Uint16Array(data));
// Output
var KixCode = strData
document.getElementById("output").innerText = KixCode;
}
<div>
<label for="KixCode">Kix Code: </label><input id="KixCode" type="text" value="eNqrVsoozcnxdFGyMjQwtjQwN9dRSq0oSC0qcSrNzElRskpLzClO1VEqT00syM8rVrKKNjUwMInVUSouSE3OBMpBRCxBItmZYAWGBgZmIINiawE68xw1"/><br /><br />
<button id="submit">Submit</button>
</div>
<p>
<span id="output"></span>
</p>
<script src="https://rawgit.com/nodeca/pako/master/dist/pako.js"></script>
可以使用pako的deflate
方法。问题是编码数据有一些 header 信息,这些信息提供了有关如何执行压缩的信息。该信息不包含在解压结果中(inflate
)。
你的示例数据原来是用 7 级压缩的,但默认为 deflate
method is level 6. This results in a difference in the second byte that is returned by deflate
when compared to the second byte of the data that was passed to inflate
. This second byte is part of the header。
注意:我不是很了解这一切——我只是提供以上内容供您进一步分析。重要的是选项会影响压缩的执行方式。
对于您的示例,将选项 { level: 7 }
传递给 deflate
方法将使结果与您最初将其传递给 inflate
的结果相同:
submit.onclick = function(){
var b64Data = document.getElementById("KixCode").value;
var strData = atob(b64Data);
var charData = Array.from(strData, x => x.charCodeAt(0));
var binData = new Uint8Array(charData);
var data = pako.inflate(binData);
var KixCode = String.fromCharCode.apply(null, new Uint16Array(data));
document.getElementById("output").innerText = KixCode;
// Reverse operation
var data2 = Array.from(KixCode, c => c.charCodeAt());
var binData2 = pako.deflate(data2, { level: 7 }); // <-- option
var charData2 = [...binData2];
var strData2 = charData2.map(i => String.fromCharCode(i)).join("");
var b64Data2 = btoa(strData2);
document.getElementById("verif").innerText = b64Data2 === b64Data ? "Reversal is correct" : "Reversal is not the same";
}
<div><label for="KixCode">Kix Code: </label><input id="KixCode" type="text" style="width: 100%" value="eNqrVsoozcnxdFGyMjQwtjQwN9dRSq0oSC0qcSrNzElRskpLzClO1VEqT00syM8rVrKKNjUwMInVUSouSE3OBMpBRCxBItmZYAWGBgZmIINiawE68xw1"></div>
<div><button id="submit">Submit</button></div>
<div id="output"></div>
<div id="verif"></div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/pako/1.0.11/pako.min.js" integrity="sha512-euWc/Qv8Kp0CbTX1M+Q3BvUyoOaq9Au50TT7vz3MFf5ver39ybq6zV+RngDY8eN8AIQFigxjwYv6jhoP546vfQ==" crossorigin="anonymous"></script>