Defining a byte array in the VBS byte () Introduction _vbs

Source: Internet
Author: User
At night on the QQ to see the nickname "garbled" friends answered the search to ask a question:


Is there a way to define a byte array in the VBS?
Is there a way to define a byte array in the VBS? is the byte subtype array (VarType is 8209) Note not vb!

But the answer is really unsightly, even people ask what do not understand, and the person incredibly still satisfied, really don't know what he was thinking.

It's all right, let me give you a quick answer. The first thing to figure out is what other people are asking. VBS is a weakly typed scripting language with only one type of data, called a variant, but the variant can be further divided into several seed types, which are not expanded here, as detailed in the VBScript data type. The so-called Byte subtype array (VarType is the kind of 8209) is the array of subtype byte (vbbyte + VBArray = 17 + 8192 = 8209).

For example, the Responsebody property of the Msxml2.xmlhttp object returns a byte array:
Copy Code code as follows:

' Author:demon
' website:http://demon.tw
' Date:2012/2/22
Dim http, Res
Set http = CreateObject ("Msxml2.xmlhttp")
Http.open "Get", "http://demon.tw", False
Http.send
res = Http.responsebody
WScript.Echo VarType (RES), TypeName (RES)

Again for example, ADODB. The Read method of the Stream object returns a byte array as well:
Copy Code code as follows:

' Author:demon
' website:http://demon.tw
' Date:2012/2/22
Dim ADO, data
Set ado = CreateObject ("ADODB.") Stream ")
Ado. Type = 1
Ado. Open
Ado. LoadFromFile "demon.tw"
data = ADO. Read
WScript.Echo VarType (data), TypeName (data)

So how do you define a byte array in the VBS? Some people say that is not simple, using the CByte function to force the conversion is not the line? Is this really the case, write a simple example to test:
Copy Code code as follows:

' Author:demon
' website:http://demon.tw
' Date:2012/2/22
Dim A (9), I
For i = 0 to 9
A (i) = CByte (i)
Next
WScript.Echo VarType (a), TypeName (a)

' Output 8204 variant () Program output 8204 variant (), unfortunately, this is not what we want.

So what's the right way? In fact, it appears in another method of reading and writing a binary file with a VBS, using the Msxml2.domdocument object:
Copy Code code as follows:

' Author:demon
' website:http://demon.tw
' Date:2012/2/22
Dim xmldoc, node, bytes
Set xmldoc = CreateObject ("Msxml2.domdocument")
Set node = xmldoc. createelement ("binary")
Node. DataType = "Bin.hex"
' demon.tw's hexadecimal value is
' 6D 6F 6E 2E 74 77
Node. Text = "64656d6f6e2e7477"
bytes = node. nodeTypedValue
WScript.Echo VarType (bytes), TypeName (bytes)

So the bytes variable is an array of bytes, and if you know the other way, you are welcome to enlighten me.

Original: http://demon.tw/programming/vbs-byte-array.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.