且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在JavaScript中生成Shift_JIS(SJIS)百分比编码的字符串

更新时间:2023-11-27 15:00:46

  • 您要从あいう%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.

            参考文献: