You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何在Microsoft JScript中获取字节数组的长度

在Microsoft JScript中获取字节数组长度的方法

你遇到的问题是因为两种方式生成的bytes并非JScript原生数组,而是COM/.NET对象,因此不能直接用原生数组的.length属性获取长度,需要针对不同对象调用对应的属性:

1. 针对System.Text.UTF8Encoding生成的字节数组

通过System.Text.UTF8Encoding.GetBytes_4得到的是.NET的byte[]对象,需要访问其**Length属性**(注意大小写敏感)来获取长度:

var asc = WScript.CreateObject("System.Text.UTF8Encoding");
var bytes = asc.GetBytes_4("Hello World");
var length = bytes.Length; // 获取字节数组长度
WScript.Echo(length); // 输出:11

2. 针对ADODB.Stream生成的字节数组

通过ADODB.Stream.Read()得到的是ADODB的Binary对象,需要访问其**Size属性**来获取长度:

var adoStr = WScript.CreateObject("Adodb.Stream");
adoStr.Charset = "utf-8";
adoStr.Type = 2;
adoStr.Open();
adoStr.WriteText("Hello World");
adoStr.Position = 0;
adoStr.Type = 1;
var bytes = adoStr.Read();
var length = bytes.Size; // 获取字节数组长度
WScript.Echo(length); // 输出:11
adoStr.Close();

内容的提问来源于stack exchange,提问作者JoeAdeoye

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.12 14:40:04