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)

Share the Post:
Heading photo, Metadata.

What is Metadata?

What is metadata? Well, It’s an odd concept to wrap your head around. Metadata is essentially the secondary layer of data that tracks details about the “regular” data. The regular

XDR solutions

The Benefits of Using XDR Solutions

Cybercriminals constantly adapt their strategies, developing newer, more powerful, and intelligent ways to attack your network. Since security professionals must innovate as well, more conventional endpoint detection solutions have evolved

AI is revolutionizing fraud detection

How AI is Revolutionizing Fraud Detection

Artificial intelligence – commonly known as AI – means a form of technology with multiple uses. As a result, it has become extremely valuable to a number of businesses across

AI innovation

Companies Leading AI Innovation in 2023

Artificial intelligence (AI) has been transforming industries and revolutionizing business operations. AI’s potential to enhance efficiency and productivity has become crucial to many businesses. As we move into 2023, several

data fivetran pricing

Fivetran Pricing Explained

One of the biggest trends of the 21st century is the massive surge in analytics. Analytics is the process of utilizing data to drive future decision-making. With so much of

kubernetes logging

Kubernetes Logging: What You Need to Know

Kubernetes from Google is one of the most popular open-source and free container management solutions made to make managing and deploying applications easier. It has a solid architecture that makes