VB6 - What does the following operation do: x = b(1) + b(2) * &H100&?

Viewed 137

Reading through some old code for doing some data that is streamed from a database. Not super familiar with VB6.

This is a rough idea of what the code looks like. Just not entirely sure what the value of x or y would be? What does *&H100& do? What does Chr$(b(3)) do?

Private Sub Sub1()
Dim b() as Byte

' b is then read from a file, a bytes string, or from a database object

x = 0
x = b(1) + b(2) * &H100&

y = ""
y = y & Chr$(b(3))

End Sub
2 Answers
x = b(1) + b(2) * &H100&

...converts two consecutive bytes into a 16-bit integer. The second byte is the high part of the integer. If the two bytes were (say) 10 and 20, the result would be (20 * 256) + 10, or 5130

Jim Mack gave an excellent answer already.

Some more supporting information from Type Characters:

Visual Basic supplies a set of identifier type characters that you can use in a declaration to specify the data type of a variable or constant.

& Long Dim M&

You can also define an integer literal as a hexadecimal (base 16) number with the &H prefix

So &H100& is a hexadecimal number being converted to a long. The & on the end is forcing conversion to a long to possibly prevent an overflow error.

From Chr():

Returns a String containing the character associated with the specified character code.

The Chr() function returns a variant type, while Chr$() returns a string.

Related