索引到Byte()变量时VBScript类型不匹配
代码:索引到Byte()变量时VBScript类型不匹配,vbscript,byte,Vbscript,Byte,代码: Set UTF8=CreateObject(“System.Text.UTF8编码”) x=UTF8.GetBytes_4(“你好”) WScript.Echo类型名(x) WScript.Echo x(1) 错误: C:\>cscript foo.vbs Microsoft (R) Windows Script Host Version 5.8 Copyright (C) Microsoft Corporation. All rights reserved. Byte() f
Set UTF8=CreateObject(“System.Text.UTF8编码”)
x=UTF8.GetBytes_4(“你好”)
WScript.Echo类型名(x)
WScript.Echo x(1)
错误:
C:\>cscript foo.vbs
Microsoft (R) Windows Script Host Version 5.8
Copyright (C) Microsoft Corporation. All rights reserved.
Byte()
foo.vbs(4, 1) Microsoft VBScript runtime error: Type mismatch: 'x'
这里有什么问题?如何修复此代码?您需要使用MidB()访问字节()的元素:
由于Byte()不是对象,因此使用.Item()是个坏主意。
GetBytes
不会返回VBScript(COM)数组。而且不能从VBScript中使用CLR对象的索引属性,需要使用项
属性。请尝试WScript.Echo x.Item(1)
。因为Byte()不是对象,所以使用.Item()是个坏主意。@Dai错误。COM互操作处理它。
>> Set UTF8 = CreateObject("System.Text.UTF8Encoding")
>> x = UTF8.GetBytes_4("hello")
>> For y = 1 To 1 + UBound(x)
>> z = MidB(x, y, 1)
>> WScript.Echo TypeName(z), z
>> Next
>> z = x.Item(1)
>>
String h
String e
String l
String l
String o
Error Number: 424
Error Description: Objekt erforderlich
>>