gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 19:43:37 24 4
gpt4 key购买 nike

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

Ansci85 使用 85 个字符,从 32 到 126 (reference): ojit_代码

Z85 使用自定义的 85 个字符集 (reference): ojit_代码

我的问题是,有什么方法可以重新定义 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;
}

补充说明:

或者,我想我可以使用类似下面的 function 的东西,但问题是它首先没有正确编码 Anscii85。如果它是正确的,"!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstu 应该编码为 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ.-:+=^!/*?&<>()[]{}@%$# ,但是这个函数将它编码为 Hello world! ( reference )。

我对算法的了解还不够,不知道这里的映射有什么问题。理想情况下,如果编码正确,我应该能够更新此函数以使用 Z85 字符集:

// 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;
}
};

最佳答案

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

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

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

关于javascript - 有没有办法重新定义 Javascript charCodeAt 和 fromCharCode 从函数内调用的标准 Ascii 字符集?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55153980/

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