DI Management Home > Cryptography > Binary and byte operations in VB6/VBA

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:
FFFFFFFF
Which 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 String types and Byte arrays, use the built-in VBA StrConv() function.
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 str
This should produce the results:
 97  98  99  100  101  102
abcdef

Note how the StrConv 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 sets.

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 str
This will produce the result:
 97  0  98  0  99  0  100  0  101  0  102  0 
abcdef
This 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.

VB6/VBA Code

The functions provided here in basConvert and basUnsignedWord can help you manage some of these issues in your code. Thanks to Ernie Gibbs for pointing out a subtle error in basUnsignedWord.uw_WordAdd (2008-06-25). The code in basConvert replaces the now superseded basByteUtils 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.

Related Topics

See also our pages on:

Contact

For more information or to comment on this page, please send us a message.

This page last updated 23 February 2016