如何将数组(数字)加载到Google应用程序脚本中Byte [] [英] How to load an Array (of numbers) into a google app script Byte[]

查看:140
本文介绍了如何将数组(数字)加载到Google应用程序脚本中Byte []的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在Google Apps Script中有一个数组(数字),我想将其转换为base64。我当然可以使用一个简单的开源库,例如这一个



然而GAS已经使用 Utilities.base64Encode(Byte [])
但是要调用这个函数I需要一个字节[] 不是数字[]



因此,以下脚本给出了一个错误:(无法将数组转换为(class)[]。

 函数testBase64Encode()
{
var bytes = []; $ var b $ for(var i = 0; i <255; i ++)bytes.push(i);

var x = Utilities.base64Encode(bytes)
}

正常情况下, Byte [] 直接来自Blob的 GetBytes()函数,但是在我的情况下,Array (数字)来自 zlib加密库



所以我试图找到一种方法来将该数组转换为 Byte [] ,可以使用 Utilities.base64Encode (Byte [])



我曾尝试创建一个Byte,并将它们放入Array中,但下面的代码也给出了错误:( ReferenceError:Byte未定义。

  var b1 = new Byte(); 

我做了一些更多的测试,并且似乎只要字节数值中的值值是128或更低。我发现这很奇怪,因为一个字节应该高达255。



以下代码对'字节'运行正常,但'bytes2'失败,因为有一个值大于128:

 函数testByteArrays()
{
var bytes = []; (var i = 0; i <128; i ++)bytes.push(i);

var x = Utilities.newBlob(bytes)

var bytes2 = []; (var i = 0; i <129; i ++)bytes2.push(i);

var x = Utilities.newBlob(bytes2)
}


解决方案

$ b

但是不能在二补符号...在这种情况下,字节应该在[ -128..127],其中高位保留为符号位。给定一个正整数 i ,如果它大于127,我们需要计算二进制补码。这可以通过减去2 8 8/256 )。把它应用到你的第一个例子中:

$ p $ function testBase64Encode()
{
var bytes = []; (var i = 0; i <255; i ++)bytes.push(i <128?i:i-256)的
;

var x = Utilities.base64Encode(bytes)
}

这将避免您收到的错误消息(无法将数组转换为(class)[]。)。理想情况下,您还应该确保数字值的范围从0到255。


I have an Array (of numbers) in Google Apps Script, and I want to convert it to base64. I could of course use a simple open-source library such as this one.

However GAS already provides functions for this using Utilities.base64Encode(Byte[]) but to call this function I need a Byte[] not Number[].

Therefore the following script gives an error: (Cannot convert Array to (class)[].)

function testBase64Encode()
{
  var bytes = [];
  for (var i = 0; i < 255; i++) bytes.push(i);

  var x = Utilities.base64Encode(bytes)
} 

Normally Byte[]'s come directly out of a Blob's GetBytes() function, however in my case the Array (of numbers) comes out of zlib encryption lib.

So I am trying to find a way to convert that Number Array to a Byte[], acceptable for use with Utilities.base64Encode(Byte[]).

I did try to create a Byte myself and put them into an Array, however the following code also gives an error: (ReferenceError: "Byte" is not defined.)

var b1 = new Byte();

I did some more testing, and it seems to work as long as the value in the bytes' numeric values are 128 or lower. I find this weird because a byte should go up to 255.

The following code runs fine for 'bytes' but fails for 'bytes2' because there is a value greater than 128:

function testByteArrays()
{
  var bytes = [];
  for (var i = 0; i < 128; i++) bytes.push(i);
  var x = Utilities.newBlob(bytes)

  var bytes2 = [];
  for (var i = 0; i < 129; i++) bytes2.push(i);
  var x = Utilities.newBlob(bytes2)
}

解决方案

...a byte should go up to 255.

But not in Two's complement notation... in that case, the bytes should be in the range [-128..127], where the high-order bit is reserved as a sign bit. Given a positive integer i, we need to calculate the two's complement if it is greater than 127. That can be done by subtracting 28 (256). Applying that to your first example:

function testBase64Encode()
{
  var bytes = [];
  for (var i = 0; i < 255; i++) bytes.push(i<128?i:i-256);

  var x = Utilities.base64Encode(bytes)
} 

This will avoid the error message you were receiving (Cannot convert Array to (class)[].). Ideally, you should also ensure that the number values start in the range 0..255.

这篇关于如何将数组(数字)加载到Google应用程序脚本中Byte []的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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