如何在javascript中编码/解码ascii85

问题描述 投票:7回答:3

我正在寻找Base64的替代方案,它可以在Unicode字符上正常工作。我发现ASCII85工作得很好,但我发现在JS没有代码或命令。

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

这里有在线Decoder / Encoder

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

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

我听说JQuery确实支持Base64,但它似乎不支持Ascii85

有人对JS中的Ascii85有什么了解吗?

谢谢

javascript jquery character-encoding ascii85 base85
3个回答
3
投票

使用dojo.js框架:

Decode Example + source

Encode Example + source

ascii85 Encoding using

摘要:

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

10
投票

纯JavaScript Ascii85 AKA 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);

1
投票

你可以使用我的JavaScript ASCII85 (Base85) implementation,它可以兼容字节数组和字符串

© www.soinside.com 2019 - 2024. All rights reserved.