devxlogo

Retrieving the hi/low byte/word of a value, and other operations

Retrieving the hi/low byte/word of a value, and other operations

As explained in the tip about the ARGBColor structure (look at the end of this tip for the link), we can define a structure and have its fields that point to the same memory address, but that read a different number of bytes. This makes easier to define a structure that allows us to set a value to a field, and then retrieve the single bytes/words of that value with the other fields. Here’s how we can define this structure:

 Structure IntegerTypes    ' A 64-bit integer     Dim Long0 As Long    ' Two 32-bit integers     Dim Integer0 As Integer     Dim Integer1 As Integer    ' Four 16-bit integers     Dim Short0 As Short     Dim Short1 As Short     Dim Short2 As Short     Dim Short3 As Short    ' Eight 8-bit integers     Dim Byte0 As Byte     Dim Byte1 As Byte     Dim Byte2 As Byte     Dim Byte3 As Byte     Dim Byte4 As Byte     Dim Byte5 As Byte     Dim Byte6 As Byte     Dim Byte7 As Byte    ' Low byte of a word    Function LowByte(ByVal Value As Long) As Byte        Long0 = Value        Return Byte0    End Function    ' High byte of a word    Function HighByte(ByVal Value As Long) As Byte        Long0 = Value        Return Byte1    End Function    ' Low word of a doubleword    Function LowWord(ByVal Value As Long) As Short        Long0 = Value        Return Short0    End Function    ' High word of a doubleword    Function HighWord(ByVal Value As Long) As Short        Long0 = Value        Return Short1    End FunctionEnd Structure

Here’s how you can test the IntegerTypes structure to retrieve the hi/low byte/word of a value, and to do other operations:

Dim it As IntegerTypesit.Short0 = 517                  ' hex 0205Console.WriteLine(it.Byte0)        ' => 5Console.WriteLine(it.Byte1)        ' => 2Console.WriteLine(it.LowByte(517))           ' => 5Console.WriteLine(it.HighByte(517))          ' => 2Console.WriteLine(it.LowWord(&HFFFF1000))    ' => 4096Console.WriteLine(it.HighWord(&HFFFF1000))   ' => -1

————————–
Note: this code is taken from Francesco Balena’s Programming Microsoft Visual Basic .NET book (MS Press 2002)

See also  Professionalism Starts in Your Inbox: Keys to Presenting Your Best Self in Email
devxblackblue

About Our Editorial Process

At DevX, we’re dedicated to tech entrepreneurship. Our team closely follows industry shifts, new products, AI breakthroughs, technology trends, and funding announcements. Articles undergo thorough editing to ensure accuracy and clarity, reflecting DevX’s style and supporting entrepreneurs in the tech sphere.

See our full editorial policy.

About Our Journalist