如何在javascript中对ascii85进行编解码

wgeznvg7  于 2022-12-25  发布在  Java
关注(0)|答案(4)|浏览(218)

我正在寻找Base64的替代品,它在Unicode字符上工作得很好。我已经找到了ASCII85,它工作得很好,但是我没有发现在JS中有这样的代码或命令。
我刚刚发现这个链接不适用于国际字符,也不包括解码功能。
这里有在线的Decoder/Encoder
甚至在C语言中发现codes这样做(我没有足够的JS数据处理知识来转换)。
还有一些我不知道如何运行的codes
我听说JQuery确实支持Base64,但似乎它不支持Ascii85
有人知道JS中的Ascii85吗?
谢谢

2cmtqfgy

2cmtqfgy1#

纯JavaScriptAscii 85AKA Base 85编码/解码函数:

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);
ruarlubt

ruarlubt2#

使用dojo.js框架:
Decode Example + source
编码示例+源代码
ascii85 Encoding using
总结:

dojo.require("dojox.encoding.ascii85");
var dc = dojox.encoding
var buf = dc.ascii85.decode("")
var a85 = dc.ascii85.encode("");
dfddblmv

dfddblmv3#

您可以使用我的JavaScript ASCII85 (Base85) implementation,它可以处理字节数组和字符串

zdwk9cvp

zdwk9cvp4#

对于那些想知道戴夫·布朗的答案的人(从他的答案逆向工程并测试):

function encode_ascii85(sSource) {
    var sSuffix, iStringLength, f;
    var charArray = [];

    if (!/[^\x00-\xFF]/.test(sSource)) {
        [sSource, sSuffix, iStringLength] = initForLoop(sSource);

        for (var iIndex = 0; iStringLength > iIndex; iIndex += 4) {
            f = (sSource.charCodeAt(iIndex) << 24) + (sSource.charCodeAt(iIndex + 1) << 16);
            f = f + (sSource.charCodeAt(iIndex + 2) << 8) + sSource.charCodeAt(iIndex + 3);

            appendNextChar(f, charArray);
        }
        (function truncate(oArray, b) {
            for (var m = b.length; m > 0; m--) oArray.pop();
        })(charArray, sSuffix);

        return "<~" + String.fromCharCode.apply(String, charArray) + "~>";
    } else {
        // Error Handling
    }

    function initForLoop(a) {
        var sSuffix = "\x00\x00\x00\x00".slice(a.length % 4 || 4);
        return [a += sSuffix, sSuffix, a.length];
    }

    function appendNextChar(f, oArray) {
        if (f === 0) {
            oArray.push(122);
        } else {
            var g, h, i, j, k;
            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;
            oArray.push(g + 33, h + 33, i + 33, j + 33, k + 33);
        }
    }
}

function decode_ascii85(sSource) {
    var sSuffix, d, iStringLength;
    var charArray = [];

    if ("<~" === sSource.slice(0, 2) && "~>" === sSource.slice(-2)) {
        sSource = initForLoop(sSource);

        for (var iIndex = 0; iStringLength > iIndex; iIndex += 5) {
            var x = "charCodeAt";
            var w = 255;

            d = 85 * 85 * 85 * 85 * (sSource[x](iIndex) - 33) + 85 * 85 * 85 * (sSource[x](iIndex + 1) - 33);
            d = d + 85 * 85 * (sSource[x](iIndex + 2) - 33) + 85 * (sSource[x](iIndex + 3) - 33) + (sSource[x](iIndex + 4) - 33);

            charArray.push(w & d >> 24, w & d >> 16, w & d >> 8, w & d);
        }
        (function truncate(oArray, b) {
            for (var m = b.length; m > 0; m--) oArray.pop();
        })(charArray, sSuffix);

        return String.fromCharCode.apply(String, charArray);
    } else {
        // Error Handling
    }

    function initForLoop(a) {
        var z = "replace",
            y = "slice";

        a = a[y](2, -2)[z](/\s/g, "")[z]("z", "!!!!!");
        sSuffix = "uuuuu" [y](a.length % 5 || 5);
        a += sSuffix;
        iStringLength = a.length;

        return a;
    }

}

相关问题