SoFunction
Updated on 2025-04-10

Defining byte array in VBS Introduction

In the evening, I saw a friend with the nickname "garbled code" on QQ and answered a question in Soso Question:


Is there a way to define a byte array in VBS?
Is there a way to define a byte array in VBS? It is the byte subtype array (VarType is the type of 8209). Note that it is not VB!

But the answer was really unsightly. I didn't even understand what others asked, and the man was still satisfied. I really didn't know what he was thinking.

I have nothing to do when I am free, let me give you a brief answer. First of all, you need to figure out what others are asking. VBS is a weak type scripting language. There is only one data type, called Variant, but Variant can be further divided into several subtypes. I will not expand it here. See "VBScript data type》. The so-called byte subtype array (VarType is 8209) is an array with subtype Byte (vbByte + vbArray = 17 + 8192 = 8209).

For example, the object's responseBody property returns a byte array:
Copy the codeThe code is as follows:

'Author: Demon
'Website:
'Date: 2012/2/22
Dim http, res
Set http = CreateObject("")
"GET", "", False

res =
VarType(res), TypeName(res)

For example, the object's Read method also returns a byte array:
Copy the codeThe code is as follows:

'Author: Demon
'Website:
'Date: 2012/2/22
Dim ado, data
Set ado = CreateObject("")
= 1

""
data =
VarType(data), TypeName(data)

So how do you define a byte array in VBS? Some people say that it's not simple, just use the CByte function to cast it? Is this really the case? Write a simple example to test:
Copy the codeThe code is as follows:

'Author: Demon
'Website:
'Date: 2012/2/22
Dim a(9), i
For i = 0 To 9
a(i) = CByte(i)
Next
VarType(a), TypeName(a)

'Output 8204 Variant() The program outputs 8204 Variant(), unfortunately, this is not what we want.

So what is the correct way? Actually in "Another way to read and write binary files with VBS》 has appeared in 》, using object:
Copy the codeThe code is as follows:

'Author: Demon
'Website:
'Date: 2012/2/22
Dim xmldoc, node, bytes
Set xmldoc = CreateObject("")
Set node = ("binary")
= ""
' The hexadecimal value is
'64 65 6D 6F 6E 2E 74 77
= "64656D6F6E2E7477"
bytes =
VarType(bytes), TypeName(bytes)

In this way, the bytes variable is a byte array. If you know other methods, please give me some advice.

original:/programming/