在Google AppsScript中,我正在尝试使用Utilities
类编码字节数组。
更新:此处的示例:https://script.google.com/d/15elqglhexplg64jzhjuzkfbj4dglhnzgbbj4dglhnzgbokjwz7akeeubcgcarap4y9x x
// bytes to encode
var toenc = [ 0x52 , 0x49 , 0x46 , 0x46
, 0xBC , 0xAF , 0x01 , 0x00
, 0x57 , 0x41 , 0x56 , 0x45
, 0x66 , 0x6D , 0x74 , 0x20
, 0x10 , 0x00 , 0x00 , 0x00
, 0x01 , 0x00 , 0x01 , 0x00
, 0x40 , 0x1f , 0x00 , 0x00
, 0x40 , 0x1f , 0x00 , 0x00
, 0x01 , 0x00 , 0x08 , 0x00
, 0x64 , 0x61 , 0x74 , 0x61
, 0x98 , 0xaf , 0x01 , 0x00
];
// This errs with -- Cannot convert Array to (class)[]
Logger.log(Utilities.base64EncodeWebSafe(toenc));
// OK, typing issue? Following the doc, but still get same error :-(
Logger.log(Utilities.base64EncodeWebSafe(
Utilities.newBlob(toenc).getBytes()
));
a,相同的错误无法在运行时将数组转换为(class)[] 。
如果我有一个(字节)编号(有效的字符串)数组,我可以将Utilities
类用于基本64吗?
以下脚本对您有帮助吗?如果我误解了您的问题,我深表歉意。
var toenc = [ 0x57 , 0x41 , 0x56 , 0x45
, 0x66 , 0x6D , 0x74 , 0x20
, 0x10 , 0x00 , 0x00 , 0x00
, 0x64 , 0x61 , 0x74 , 0x61
];
var a1 = Utilities.base64EncodeWebSafe(toenc);
var a2 = Utilities.base64DecodeWebSafe(a1, Utilities.Charset.UTF_8);
var a3 = Utilities.newBlob(a2).getDataAsString();
>>> a1 = V0FWRWZtdCAQAAAAZGF0YQ==
>>> a2 = [87, 65, 86, 69, 102, 109, 116, 32, 16, 0, 0, 0, 100, 97, 116, 97]
>>> a3 = WAVEfmt ���data
找到了答案。它涉及该功能想要2的赞美号的事实。解决方案:
function to64(arr) {
var bytes = [];
for (var i = 0; i < arr.length; i++)
bytes.push(arr[i]<128?arr[i]:arr[i]-256);
return Utilities.base64EncodeWebSafe(bytes)
} // to64
https://stackoverflow.com/a/20639942/199305