Atlanta Custom Software Development 

 
   Search        Code/Page
 

User Login
Email

Password

 

Forgot the Password?
Services
» Web Development
» Maintenance
» Data Integration/BI
» Information Management
Programming
  Database
Automation
OS/Networking
Graphics
Links
Tools
» Regular Expr Tester
» Free Tools

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

Total Hit ( 4241)

Rate this article:     Poor     Excellent 

 Submit Your Question/Comment about this article

Rating


 


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:

Click here to copy the following block
<StructLayout(LayoutKind.Explicit)> Structure IntegerTypes
  ' A 64-bit integer
  <FieldOffset(0)> Dim Long0 As Long
  ' Two 32-bit integers
  <FieldOffset(0)> Dim Integer0 As Integer
  <FieldOffset(4)> Dim Integer1 As Integer
  ' Four 16-bit integers
  <FieldOffset(0)> Dim Short0 As Short
  <FieldOffset(2)> Dim Short1 As Short
  <FieldOffset(4)> Dim Short2 As Short
  <FieldOffset(6)> Dim Short3 As Short
  ' Eight 8-bit integers
  <FieldOffset(0)> Dim Byte0 As Byte
  <FieldOffset(1)> Dim Byte1 As Byte
  <FieldOffset(2)> Dim Byte2 As Byte
  <FieldOffset(3)> Dim Byte3 As Byte
  <FieldOffset(4)> Dim Byte4 As Byte
  <FieldOffset(5)> Dim Byte5 As Byte
  <FieldOffset(6)> Dim Byte6 As Byte
  <FieldOffset(7)> 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 Function

End 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:

Click here to copy the following block
Dim it As IntegerTypes

it.Short0 = 517         ' hex 0205
Console.WriteLine(it.Byte0)    ' => 5
Console.WriteLine(it.Byte1)    ' => 2

Console.WriteLine(it.LowByte(517))      ' => 5
Console.WriteLine(it.HighByte(517))     ' => 2
Console.WriteLine(it.LowWord(&HFFFF1000))  ' => 4096
Console.WriteLine(it.HighWord(&HFFFF1000))  ' => -1


Submitted By : Nayan Patel  (Member Since : 5/26/2004 12:23:06 PM)

Job Description : He is the moderator of this site and currently working as an independent consultant. He works with VB.net/ASP.net, SQL Server and other MS technologies. He is MCSD.net, MCDBA and MCSE. In his free time he likes to watch funny movies and doing oil painting.
View all (893) submissions by this author  (Birth Date : 7/14/1981 )


Home   |  Comment   |  Contact Us   |  Privacy Policy   |  Terms & Conditions   |  BlogsZappySys

© 2008 BinaryWorld LLC. All rights reserved.