Number System Functions

#### Returns a string representing the hexadecimal equivalent of x (i.e., it converts a decimal number to a hexadecimal number).

Example:

Print "The decimal number 687 is "; Hex\$(687); " in hex."

The output of the line above would be:

The decimal number 687 is 2AF in hex.

#### Returns a string representing the octal equivalent of x (i.e., it converts a decimal number to a octal number).

Example:

Print "The decimal number 627 is "; Oct\$(627); " in octal."

The output of the line above would be:

The decimal number 627 is 1163 in octal.

### Hex and Octal Numeric Literals

&H before a number means that the number is specified in hex:

Example:

Const lngHexNum As Long = &H2AF

Print lngHexNum

Output:

687

&O before a number means that the number is specified in octal:

Example:

Const lngOctNum As Long = &O1163

Print lngOctNum

Output:

627

Converting a Hex or Octal Number to Decimal

To convert a hexadecimal or octal number to a decimal value, you must treat the hex or octal value as a string, append "&H" or "&O" in front of the value, and use a conversion function such as Val or CLng to convert the string to a numeric value.

Example:

Dim strMyHexNum      As String

Dim lngDecimalValue  As Long

strMyHexNum = InputBox("Enter a Hex Value:")

strMyHexNum = "&H" & strMyHexNum

lngDecimalValue = Val(strMyHexNum)

PRINT strMyHexNum; " in hex is equivalent to "; lngDecimalValue; " in decimal."

If the user entered  "2AF" as input, the result would be:

2AF in hex is equivalent to 687 in decimal.

The following "Try It" code illustrates the concepts presented above:

Private Sub cmdTryIt_Click()

Const lngDECIMAL_NUMBER_1   As Long = 687

Const lngDECIMAL_NUMBER_2   As Long = 627

Const lngHEXADECIMAL_NUMBER As Long = &H2AF

Const lngOCTAL_NUMBER       As Long = &O1163

Const strHEX_STRING         As String = "BEEF"

Const strOCT_STRING         As String = "411"

Dim strHexVal               As String

Dim strOctVal               As String

Dim lngDecVal               As Long

strHexVal = Hex\$(lngDECIMAL_NUMBER_1)

Print "The decimal number "; lngDECIMAL_NUMBER_1; " is "; strHexVal; " in hex."

strOctVal = Oct\$(lngDECIMAL_NUMBER_2)

Print "The decimal number "; lngDECIMAL_NUMBER_2; " is "; strOctVal; " in octal."

Print "The Long Constant specified as '&H2AF' evaluates to: "; lngHEXADECIMAL_NUMBER

Print "The Long Constant specified as '&O1163' evaluates to: "; lngOCTAL_NUMBER

lngDecVal = CLng("&H" & strHEX_STRING)

Print strHEX_STRING; " in hex is equivalent to "; lngDecVal; " in decimal."

lngDecVal = CLng("&O" & strOCT_STRING)

Print strOCT_STRING; " in octal is equivalent to "; lngDecVal; " in decimal."

End Sub

Output:

Download the VB project code for the example above here.

If you wish to review number systems theory, a comprehensive tutorial in PDF format is available via the "Number Systems Tutorial" link below. This tutorial covers the binary, hexadecimal, and octal number systems – including how to convert from one number system to the other and how to perform addition and subtraction in these systems.

You will need the Adobe Reader to view the tutorial. If you do not have the Adobe Reader installed on your system, you can download it here.

Number Systems Tutorial (PDF format, 1141 KB)