如何在JavaScript中生成Shift_JIS(SJIS)百分比编码的字符串 [英] How to generate a Shift_JIS(SJIS) percent encoded string in JavaScript

查看:1052
本文介绍了如何在JavaScript中生成Shift_JIS(SJIS)百分比编码的字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JavaScript和Google Apps脚本都是陌生的,并且在将单元格中写入的文本转换为Shift-JIS(SJIS)编码的字母时遇到问题. 例如,日语字符串あいう"应编码为%82%A0%82%A2%82%A4",而不应编码为%E3%81%82%E3%81%84%E3%81%86"是UTF-8编码的.

I'm new to both JavaScript and Google Apps Script and having a problem to convert texts written in a cell to the Shift-JIS (SJIS) encoded letters. For example, the Japanese string "あいう" should be encoded as "%82%A0%82%A2%82%A4" not as "%E3%81%82%E3%81%84%E3%81%86" which is UTF-8 encoded.

我尝试了EncodingJS和内置的urlencode()函数,但是它们都返回了UTF-8编码的一个.

I tried EncodingJS and the built-in urlencode() function but it both returns the UTF-8 encoded one.

有人可以告诉我如何在GAS中正确获取SJIS编码的字母吗?谢谢.

Would any one tell me how to get the SJIS-encoded letters properly in GAS? Thank you.

推荐答案

  • 您要从あいう%82%A0%82%A2%82%A4的URL编码为字符集的Shift-JIS.
    • %E3%81%82%E3%81%84%E3%81%86是转换为UTF-8的结果.
      • You want to do the URL encode from あいう to %82%A0%82%A2%82%A4 as Shift-JIS of the character set.
        • %E3%81%82%E3%81%84%E3%81%86 is the result converted as UTF-8.
        • 如果我的理解是正确的,那么这个答案如何?请认为这只是几个可能的答案之一.

          If my understanding is correct, how about this answer? Please think of this as just one of several possible answers.

          • 为了使用Google Apps脚本中字符集的Shift-JIS,需要将其用作二进制数据.因为,当Google Apps脚本检索Shift-JIS的值作为字符串时,字符集会自动更改为UTF-8.请注意这一点.
          • In order to use Shift-JIS of the character set at Google Apps Script, it is required to use it as the binary data. Because, when the value of Shift-JIS is retrieved as the string by Google Apps Script, the character set is automatically changed to UTF-8. Please be careful this.

          为了从あいう转换为%82%A0%82%A2%82%A4,下面的脚本怎么样?在这种情况下,该脚本可用于平假名字符.

          In order to convert from あいう to %82%A0%82%A2%82%A4, how about the following script? In this case, this script can be used for HIRAGANA characters.

          function muFunction() {
            var str = "あいう";
          
            var bytes = Utilities.newBlob("").setDataFromString(str, "Shift_JIS").getBytes();
            var res = bytes.map(function(byte) {return "%" + ("0" + (byte & 0xFF).toString(16)).slice(-2)}).join("").toUpperCase();
            Logger.log(res)
          }
          

          结果:

          您可以在日志中看到以下结果.

          Result:

          You can see the following result at the log.

          %82%A0%82%A2%82%A4
          

          示例脚本2:

          如果要转换包含汉字字符的值,那么以下脚本如何?在这种情况下,本日は晴天なり会转换为%96%7B%93%FA%82%CD%90%B0%93V%82%C8%82%E8.

          function muFunction() {
            var str = "本日は晴天なり";
            var conv = Utilities.newBlob("0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz*-.@_").getBytes().map(function(e) {return ("0" + (e & 0xFF).toString(16)).slice(-2)});
            var bytes = Utilities.newBlob("").setDataFromString(str, "Shift_JIS").getBytes();
            var res = bytes.map(function(byte) {
              var n = ("0" + (byte & 0xFF).toString(16)).slice(-2);
              return conv.indexOf(n) != -1 ? String.fromCharCode(parseInt(n[0], 16).toString(2).length == 4 ? parseInt(n, 16) - 256 : parseInt(n, 16)) : ("%" + n).toUpperCase();
            }).join("");
            Logger.log(res)
          }
          

          结果:

          您可以在日志中看到以下结果.

          Result:

          You can see the following result at the log.

          %96%7B%93%FA%82%CD%90%B0%93V%82%C8%82%E8
          

          • 用示例脚本1转换本日は晴天なり时,它变得像%96%7B%93%FA%82%CD%90%B0%93%56%82%C8%82%E8.这也可以解码.但是似乎通常使用通过示例脚本2转换的结果值.
            • When 本日は晴天なり is converted with the sample script 1, it becomes like %96%7B%93%FA%82%CD%90%B0%93%56%82%C8%82%E8. This can also decoded. But it seems that the result value converted with the sample script 2 is generally used.
            • 此脚本的流程如下.

              1. 创建新的blob作为空数据.
              2. あいう的文本值放入Blob.那时,文本值作为字符集的Shift-JIS放置.
                • 在这种情况下,即使使用blob.getDataAsString("Shift_JIS"),结果也将变为UTF-8.因此,需要将blob用作二进制数据,而不转换为字符串数据.这是此答案中的重点.
              1. Create new blob as the empty data.
              2. Put the text value of あいう to the blob. At that time, the text value is put as Shift-JIS of the the character set.
                • In this case, even when blob.getDataAsString("Shift_JIS") is used, the result becomes UTF-8. So the blob is required to be used as the binary data without converting to the string data. This is the important point in this answer.
              • 在Google Apps脚本中,字节数组使用的是他的十六进制符号.因此需要转换为无符号十六进制.
              • 当值为汉字字符时,如果可以将2个字节的字符转换为字符串值作为ASCII码,则需要使用字符串值.在这种情况下,可以使用示例脚本2"的脚本.
                • 在上面的示例中,变为%93V.
                • At Google Apps Script, the byte array is uses as he signed hexadecimal. So it is required to convert to the unsigned hexadecimal.
                • When the value is the KANJI character, when the characters of 2 bytes can be converted to the string value as the ascii code, the string value is required to be used. The script of "Sample script 2" can be used for this situation.
                  • At above sample, becomes %93V.

                  参考文献:

                  • newBlob(数据)
                  • setDataFromString(string,charset)
                  • getBytes()
                  • map()
                  • References:

                    • newBlob(data)
                    • setDataFromString(string, charset)
                    • getBytes()
                    • map()
                    • 如果我误解了你的问题,而这不是你想要的方向,我深表歉意.

                      If I misunderstood your question and this was not the direction you want, I apologize.

                      这篇关于如何在JavaScript中生成Shift_JIS(SJIS)百分比编码的字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆