Someone asked me how could a Hex be represented in Visual Basic 6. Fortunately, there is a function that does this for us already. Its called Hex(), or Hex$(). It returns a string representing the hexadecimal equivalent of an integer. Of course, I prefer the C style representation of hex (either 0x00 or 00h), so I created a function that does this for me. Below is the code demonstrating this. For this, there is a small form with a textbox, a checkbox, and a label. The textbox will take in a number, the checkbox will determine which format to use, the long format of “0x00” or the short format of “00h”, and the label will display the result.
Function convertToHex(n As Integer, longStyle As Boolean) As String
If (longStyle) Then
convertToHex = "0x" & Hex(n)
convertToHex = Hex(n) & "h"
Private Sub Command1_Click()
If (Check1.Value = Checked) Then
Label1.Caption = convertToHex(Text1.Text, True)
Label1.Caption = convertToHex(Text1.Text, False)
Private Sub Form_Load()
Text1.Text = &HFF
Also notice in the Form_Load() method, I am hard coding the value of 255 (0xFF) into the textbox. The prefix of &H will force VB to take in Hex, but the value displayed in the textbox will be 255.