Binary and byte operations in VB6/VBA
Doing cryptographic operations in Classic Visual Basic 6 and VBA is tricky because VB6 doesn't
have unsigned types (except the Byte type).
That da*n sign bit keeps messing things up. A simple operation in C like
x<<=8 is not a simple matter with a Long variable in VB6.
Try this experiment:
Dim lngX As Long lngX = &HFFFF ' Half a long? Debug.Print Hex(lngX)This will "carry over" the sign bit into the high word and print:
FFFFFFFFWhich is not what we expect. The correct way is to append an ampersand (&) to cast it as a proper long:
Dim lngX As Long lngX = &HFFFF& ' Correct Debug.Print Hex(lngX)Likewise, migrating values from string types to bytes and words has its difficulties. To convert back-and-forth between
Bytearrays, use the built-in VBA
Dim i As Long Dim x() As Byte Dim str As String ' Convert string to ANSI byte array x = StrConv("abcdef", vbFromUnicode) For i = 0 To UBound(x) Debug.Print x(i); Next Debug.Print ' Convert back to a string str = StrConv(x(), vbUnicode) Debug.Print strThis should produce the results:
97 98 99 100 101 102 abcdef
Note how the
function avoids the need to declare the size of the byte array beforehand.
(Thanks to Robert Garofalo for pointing out this useful function).
For more information see Using StrConv with ANSI, DBCS and Unicode character
If you want to convert to a byte array without the Unicode conversion, do this:
Dim i As Long Dim x() As Byte Dim str As String str = "abcdef" ' Copy string to byte array without Unicode conversion x = str For i = 0 To UBound(x) Debug.Print x(i); Next Debug.Print ' Convert back to a string str = x Debug.Print strThis will produce the result:
97 0 98 0 99 0 100 0 101 0 102 0 abcdefThis time, we get two bytes for each Unicode character. See Microsoft Knowledge Base Article - 187675 HOWTO: Copy a String to a Byte Array Without Unicode Conversion for more details.
If you want the string encoded in UTF-8, see How to convert VBA/VB6 Unicode strings to UTF-8.
The functions provided here in
can help you manage some of these issues in your code.
Thanks to Ernie Gibbs for pointing out a subtle error in
The code in basConvert replaces the now superseded
with better and faster dynamic arrays.
However, the functions in basByteUtils are still useful for ASP and VBScript applications.
The source code to these three files is in binaryutils.vb6.zip (9 kB).
Note: Users with operating systems that are set up to use full 32-bit Unicode OS or oriental CJK characters may need to do a global replace of Asc() and Chr() with AscW() and ChrW() in the basConvert and basByteUtils functions above. Thanks to David Wolf of Intuit Information Technology Solutions for this tip.
For more hints on how to use arrays of the Byte type in Visual Basic 6 compared to the simpler String type, see Using Byte Arrays in VB6/VBA.
- How to convert VBA/VB6 Unicode strings to UTF-8
- Cross-Platform Encryption
- Storing and representing ciphertext
- Using Byte Arrays in VB6/VBA
- Cryptography with International Character Sets
For more information or to comment on this page, please send us a message.
This page last updated 23 February 2016