更新时间: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
的结果.あいう
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.
Shift-JIS
,需要将其用作二进制数据.因为,当Google Apps脚本检索Shift-JIS
的值作为字符串时,字符集会自动更改为UTF-8
.请注意这一点.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
如果要转换包含汉字字符的值,那么以下脚本如何?在这种情况下,本日は晴天なり
会转换为%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
本日は晴天なり
时,它变得像%96%7B%93%FA%82%CD%90%B0%93%56%82%C8%82%E8
.这也可以解码.但是似乎通常使用通过示例脚本2转换的结果值.
本日は晴天なり
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.此脚本的流程如下.
あいう
的文本值放入Blob.那时,文本值作为字符集的Shift-JIS
放置.
blob.getDataAsString("Shift_JIS")
,结果也将变为UTF-8
.因此,需要将blob用作二进制数据,而不转换为字符串数据.这是此答案中的重点.あいう
to the blob. At that time, the text value is put as Shift-JIS
of the the character set.
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.天
变为%93V
.天
becomes %93V
.
如果我误解了你的问题,而这不是你想要的方向,我深表歉意.
If I misunderstood your question and this was not the direction you want, I apologize.