有没有办法重新定义Javascript charCodeAt和fromCharCode在函数中调用的标准Ascii字符集?

有没有办法重新定义Javascript charCodeAt和fromCharCode在函数中调用的标准Ascii字符集?,javascript,Javascript,对于编码,Javascript从标准Anscii表中提取字符映射。我发现下面的代码非常出色并且正确地编码到Anscii85/Base85。但我想对Z85变体进行编码,因为它包含我需要的一组符号。我的理解是,Anscii85/Base85编码的工作原理应该完全相同,只是Z85以不同于Anscii标准的顺序映射值,并使用不同于标准Ansii85映射的符号组合。因此,字符集是唯一的区别: Ansci85使用85个字符,从32到126(): “!\”\$%&'()*+,-./0123456789:@ab

对于编码,Javascript从标准Anscii表中提取字符映射。我发现下面的代码非常出色并且正确地编码到Anscii85/Base85。但我想对Z85变体进行编码,因为它包含我需要的一组符号。我的理解是,Anscii85/Base85编码的工作原理应该完全相同,只是Z85以不同于Anscii标准的顺序映射值,并使用不同于标准Ansii85映射的符号组合。因此,字符集是唯一的区别:

Ansci85使用85个字符,从32到126():
“!\”\$%&'()*+,-./0123456789:@abcdefghijklmnopqrstuvxyz[\\]^`abcdefghijklmnopqrstu

Z85使用85个字符的自定义集():
0123456789abcdefghijklmnopqrstuvxyzabcdefghijklmnopqrstuvxyz.-:+=^!/*&()[]{}@%$#

我的问题是,有没有办法重新定义charCodeAt和fromCharCode在此函数中引用的字符集,以便它在Z85中编码

// By Steve Hanov. Released to the public domain.
function encodeAscii85(input) {
// Remove Adobe standard prefix
//  var output = "<~";
  var chr1, chr2, chr3, chr4, chr, enc1, enc2, enc3, enc4, enc5;
  var i = 0;

  while (i < input.length) {
    // Access past the end of the string is intentional.
    chr1 = input.charCodeAt(i++);
    chr2 = input.charCodeAt(i++);
    chr3 = input.charCodeAt(i++);
    chr4 = input.charCodeAt(i++);

    chr = ((chr1 << 24) | (chr2 << 16) | (chr3 << 8) | chr4) >>> 0;

    enc1 = (chr / (85 * 85 * 85 * 85) | 0) % 85 + 33;
    enc2 = (chr / (85 * 85 * 85) | 0) % 85 + 33;
    enc3 = (chr / (85 * 85) | 0 ) % 85 + 33;
    enc4 = (chr / 85 | 0) % 85 + 33;
    enc5 = chr % 85 + 33;

    output += String.fromCharCode(enc1) +
      String.fromCharCode(enc2);
    if (!isNaN(chr2)) {
      output += String.fromCharCode(enc3);
      if (!isNaN(chr3)) {
        output += String.fromCharCode(enc4);
        if (!isNaN(chr4)) {
          output += String.fromCharCode(enc5);
        }
      }
    }
  }
// Remove Adobe standard suffix
//  output += "~>";

  return output;
}

字符代码是字符代码。无法更改
String.fromCharCode()
String.charCodeAt()
的行为

但是,您可以将自定义字符集存储在数组中,并使用数组索引和
array.indexOf()
查找条目


但是,更新此函数以使用Z85将是一件棘手的事情,因为
String.fromCharCode()
String.charCodeAt()
在两种不同的上下文中使用——它们有时用于访问未编码的字符串(不需要更改),有时用于访问编码的字符串(需要更改)。您需要注意不要混淆两者。

谢谢。这很有帮助。让我根据你的信息玩一会儿。从理论上讲,我应该能够继续使用fromCharCode原样来提取未编码的字符串,但用数组索引替换charCodeAt以映射到z85字符集……这是可行的,只是我的编码结果不太正确。我想我可能只需要考虑零和空格。但是您的回答允许我像这样成功地更改字符集:我创建了字母表数组
var alphabet=[“0”、“1”、“2”等]
,从var计算中删除了字符偏移量33,例如
enc5=chr%85+33,然后用字母表[var]
替换了
String.fromCharCode(var)
。我猜出来了。当我通过拆分整个字符串来简化数组构建时,它输出了正确的编码。我的第一个字符数组中一定有错误。我不知道为什么我在发布我的原始问题之前没有考虑这个方法。我想是想得太多了吧!再次感谢!
// Adapted from: Ascii85 JavaScript implementation, 2012.10.16 Jim Herrero
// Original: https://jsfiddle.net/nderscore/bbKS4/
var Ascii85 = {
    // Ascii85 mapping
    _alphabet: "!\"#$%&'()*+,-./0123456789:;<=>?@"+
               "ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`"+
               "abcdefghijklmnopqrstu"+

               "y"+ // short form 4 spaces (optional)
               "z", // short form 4 nulls (optional)

    // functions
    encode: function(input) {
        var alphabet = Ascii85._alphabet,
            useShort = alphabet.length > 85,
            output = "", buffer, val, i, j, l;

        for (i = 0, l = input.length; i < l;) {
            buffer = [0,0,0,0];
            for (j = 0; j < 4; j++)
                if(input[i])
                  buffer[j] = input.charCodeAt(i++);

            for (val = buffer[3], j = 2; j >= 0; j--)
                val = val*256+buffer[j];

            if (useShort && !val) 
                output += alphabet[86];
            else if (useShort && val == 0x20202020) 
                output += alphabet[85];
            else {
                for (j = 0; j < 5; j++) {
                    output += alphabet[val%85];
                    val = Math.floor(val/85);
                }
            }
        }

        return output;
    }
};