更改 JavaScript 字符串编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/18879860/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 13:32:13  来源:igfitidea点击:

Change JavaScript string encoding

javascriptjquerystringencoding

提问by David

At the moment I have a large JavaScript string I'm attempting to write to a file, but in a different encoding (ISO-8859-1). I was hoping to use something like downloadify. Downloadify only accepts normal JavaScript strings or base64 encoded strings.

目前我有一个很大的 JavaScript 字符串,我正在尝试写入文件,但使用不同的编码 (ISO-8859-1)。我希望使用类似downloadify 的东西。Downloadify 只接受普通的 JavaScript 字符串或 base64 编码的字符串。

Because of this, I've decided to compress my string using JSZipwhich generates a nicely base64 encoded string that can be passed to downloadify, and downloaded to my desktop. Huzzah! The issue is that the string I compressed, of course, is still the wrong encoding.

因此,我决定使用JSZip压缩我的字符串,它会生成一个很好的 base64 编码字符串,可以传递给 downloadify,然后下载到我的桌​​面。哈扎!问题是我压缩的字符串,当然,仍然是错误的编码。

Luckily JSZip can take a Uint8Array as data, instead of a string. So is there any way to convert a JavaScript string into a ISO-8859-1 encoded string and store it in a Uint8Array?

幸运的是 JSZip 可以将 Uint8Array 作为数据,而不是字符串。那么有什么方法可以将 JavaScript 字符串转换为 ISO-8859-1 编码的字符串并将其存储在 Uint8Array 中?

Alternatively, if I'm approaching this all wrong, is there a better solution all together? Is there a fancy JavaScript string class that can use different internal encodings?

或者,如果我的方法都错了,是否有更好的解决方案?是否有一个花哨的 JavaScript 字符串类可以使用不同的内部编码?

Edit: To clarify, I'm not pushing this string to a webpage so it won't automatically convert it for me. I'm doing something like this:

编辑:澄清一下,我没有将此字符串推送到网页,因此它不会自动为我转换它。我正在做这样的事情:

var zip = new JSZip();
zip.file("genSave.txt", result);

return zip.generate({compression:"DEFLATE"});

And for this to make sense, I would need result to be in the proper encoding (and JSZip only takes strings, arraybuffers, or uint8arrays).

为了使这一点有意义,我需要结果采用正确的编码(并且 JSZip 只接受字符串、数组缓冲区或 uint8arrays)。

Final Edit (This was -not- a duplicate question because the result wasn't being displayed in the browser or transmitted to a server where the encoding could be changed):

最终编辑(这不是一个重复的问题,因为结果没有显示在浏览器中或传输到可以更改编码的服务器):

This turned out to be a little more obscure than I had thought, so I ended up rolling my own solution. It's not nearly as robust as a proper solution would be, but it'll convert a JavaScript string into windows-1252 encoding, and stick it in a Uint8Array:

结果证明这比我想象的要模糊一些,所以我最终推出了自己的解决方案。它不像适当的解决方案那么健壮,但它会将 JavaScript 字符串转换为 windows-1252 编码,并将其粘贴到 Uint8Array 中:

var enc = new string_transcoder("windows-1252");
var tenc = enc.transcode(result); //This is now a Uint8Array

You can then either use it in the array like I did:

然后你可以像我一样在数组中使用它:

//Make this into a zip
var zip = new JSZip();   
zip.file("genSave.txt", tenc);   
return zip.generate({compression:"DEFLATE"});

Or convert it into a windows-1252 encoded string using this string encoding library:

或使用此字符串编码库将其转换为 windows-1252 编码的字符串:

var string = TextDecoder("windows-1252").decode(tenc);

To use this function, either use:

要使用此功能,请使用:

<script src="//www.eu4editor.com/string_transcoder.js"></script>

Or include this:

或者包括这个:

function string_transcoder (target) {

    this.encodeList = encodings[target];
    if (this.encodeList === undefined) {
        return undefined;
    }

    //Initialize the easy encodings
    if (target === "windows-1252") {
        var i;
        for (i = 0x0; i <= 0x7F; i++) {
            this.encodeList[i] = i;          
        }
        for (i = 0xA0; i <= 0xFF; i++) {
            this.encodeList[i] = i;
        }
    }

}

string_transcoder.prototype.transcode = function (inString) {


    var res = new Uint8Array(inString.length), i;


    for (i = 0; i < inString.length; i++) {
        var temp = inString.charCodeAt(i);
        var tempEncode = (this.encodeList)[temp];
        if (tempEncode === undefined) {
            return undefined; //This encoding is messed up
        } else {
            res[i] = tempEncode;
        }
    }

    return res;
};

encodings = {

    "windows-1252": {0x20AC:0x80, 0x201A:0x82, 0x0192:0x83, 0x201E:0x84, 0x2026:0x85, 0x2020:0x86, 0x2021:0x87, 0x02C6:0x88, 0x2030:0x89, 0x0160:0x8A, 0x2039:0x8B, 0x0152:0x8C, 0x017D:0x8E, 0x2018:0x91, 0x2019:0x92, 0x201C:0x93, 0x201D:0x94, 0x2022:0x95, 0x2013:0x96, 0x2014:0x97, 0x02DC:0x98, 0x2122:0x99, 0x0161:0x9A, 0x203A:0x9B, 0x0153:0x9C, 0x017E:0x9E, 0x0178:0x9F}     

};

采纳答案by Nate

This turned out to be a little more obscure than [the author] had thought, so [the author] ended up rolling [his] own solution. It's not nearly as robust as a proper solution would be, but it'll convert a JavaScript string into windows-1252 encoding, and stick it in a Uint8Array:

事实证明这比[作者]想象的要模糊一些,所以[作者]最终推出了[他]自己的解决方案。它不像适当的解决方案那么健壮,但它会将 JavaScript 字符串转换为 windows-1252 编码,并将其粘贴到 Uint8Array 中:

var enc = new string_transcoder("windows-1252");
var tenc = enc.transcode(result); //This is now a Uint8Array

You can then either use it in the array like [the author] did:

然后,您可以像 [作者] 那样在数组中使用它:

//Make this into a zip
var zip = new JSZip();   
zip.file("genSave.txt", tenc);   
return zip.generate({compression:"DEFLATE"});

Or convert it into a windows-1252 encoded string using this string encoding library:

或使用此字符串编码库将其转换为 windows-1252 编码的字符串:

var string = TextDecoder("windows-1252").decode(tenc);

To use this function, either use:

要使用此功能,请使用:

<script src="//www.eu4editor.com/string_transcoder.js"></script>

Or include this:

或者包括这个:

function string_transcoder (target) {

    this.encodeList = encodings[target];
    if (this.encodeList === undefined) {
        return undefined;
    }

    //Initialize the easy encodings
    if (target === "windows-1252") {
        var i;
        for (i = 0x0; i <= 0x7F; i++) {
            this.encodeList[i] = i;          
        }
        for (i = 0xA0; i <= 0xFF; i++) {
            this.encodeList[i] = i;
        }
    }

}

string_transcoder.prototype.transcode = function (inString) {


    var res = new Uint8Array(inString.length), i;


    for (i = 0; i < inString.length; i++) {
        var temp = inString.charCodeAt(i);
        var tempEncode = (this.encodeList)[temp];
        if (tempEncode === undefined) {
            return undefined; //This encoding is messed up
        } else {
            res[i] = tempEncode;
        }
    }

    return res;
};

encodings = {

    "windows-1252": {0x20AC:0x80, 0x201A:0x82, 0x0192:0x83, 0x201E:0x84, 0x2026:0x85, 0x2020:0x86, 0x2021:0x87, 0x02C6:0x88, 0x2030:0x89, 0x0160:0x8A, 0x2039:0x8B, 0x0152:0x8C, 0x017D:0x8E, 0x2018:0x91, 0x2019:0x92, 0x201C:0x93, 0x201D:0x94, 0x2022:0x95, 0x2013:0x96, 0x2014:0x97, 0x02DC:0x98, 0x2122:0x99, 0x0161:0x9A, 0x203A:0x9B, 0x0153:0x9C, 0x017E:0x9E, 0x0178:0x9F}     

};

回答by user2511140

Test the following script:

测试以下脚本:

<script type="text/javascript" charset="utf-8">