gpt4 book ai didi

javascript - 如何在javascript中编码/解码ascii85

转载 作者:数据小太阳 更新时间:2023-10-29 04:30:07 30 4
gpt4 key购买 nike

我一直在寻找 Base64 的替代方法,它可以很好地处理 Unicode 字符。我发现 ASCII85 效果很好,但是我发现在 JS 中没有执行此操作的代码或命令。

我刚找到 this link它不适用于国际字符并且不包括解码功能。

这里有在线Decoder/Encoder .

甚至找到codes在 C 语言中这样做(我没有足够的 JS 数据处理知识来转换)。

还有一些codes我不知道怎么跑。

听说JQuery支持Base64,但好像不支持Ascii85

有人知道关于 JS 中的 Ascii85 的任何信息吗?

谢谢

最佳答案

纯 JavaScript Ascii85 又名 Base85 编码/解码函数:

function encode_ascii85(a) {
var b, c, d, e, f, g, h, i, j, k;
for (!/[^\x00-\xFF]/.test(a), b = "\x00\x00\x00\x00".slice(a.length % 4 || 4), a += b,
c = [], d = 0, e = a.length; e > d; d += 4) f = (a.charCodeAt(d) << 24) + (a.charCodeAt(d + 1) << 16) + (a.charCodeAt(d + 2) << 8) + a.charCodeAt(d + 3),
0 !== f ? (k = f % 85, f = (f - k) / 85, j = f % 85, f = (f - j) / 85, i = f % 85,
f = (f - i) / 85, h = f % 85, f = (f - h) / 85, g = f % 85, c.push(g + 33, h + 33, i + 33, j + 33, k + 33)) :c.push(122);
return function(a, b) {
for (var c = b; c > 0; c--) a.pop();
}(c, b.length), "<~" + String.fromCharCode.apply(String, c) + "~>";
}

function decode_ascii85(a) {
var c, d, e, f, g, h = String, l = "length", w = 255, x = "charCodeAt", y = "slice", z = "replace";
for ("<~" === a[y](0, 2) && "~>" === a[y](-2), a = a[y](2, -2)[z](/\s/g, "")[z]("z", "!!!!!"),
c = "uuuuu"[y](a[l] % 5 || 5), a += c, e = [], f = 0, g = a[l]; g > f; f += 5) d = 52200625 * (a[x](f) - 33) + 614125 * (a[x](f + 1) - 33) + 7225 * (a[x](f + 2) - 33) + 85 * (a[x](f + 3) - 33) + (a[x](f + 4) - 33),
e.push(w & d >> 24, w & d >> 16, w & d >> 8, w & d);
return function(a, b) {
for (var c = b; c > 0; c--) a.pop();
}(e, c[l]), h.fromCharCode.apply(h, e);
}

var myString='This is a test!';
var encoded=encode_ascii85(myString);
var decoded=decode_ascii85(encoded);
document.write(decoded);

关于javascript - 如何在javascript中编码/解码ascii85,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17184813/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com