VB6 Number System Functions (Hex, Oct, Etc)

Level:
Level2

Written By TheVBProgramer.

$ReqTestHarness$

Function

Description

Hex$(x)

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.

 

Oct$(x)

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.

add two hex value

how can we add two hex values from 2 text boxes and print the sum of them into another text box?

YEAH!!

THANKSSSS!~~~ WORKS GREAT!!!!!!!!!

math functions

how do you do "does not divide by X"?
like i know in python you do %
for example:

if X % 2 then
x*3
end if

It helped me.

I was getting a value returned in negative. This tool gave me a hint to use cLng() function .

converter for decimal to hexadecimal

sir,mom

can you gave me a program that coverts a decimal value to hexadecimal
but not more than 255 but not less than on 0

if there a program just e-mail me on my yahoomail in johnrob17k@yahoo.com

i will wait for your response

can you do that in TextBox??

can you do that in TextBox??

Great! Thanks.

Great! Thanks.

It's Unsigned

Using the "&H" or the "&O" prefixes returns unsigned values. If you are in range where that could be an issue (for example 2 to the 16th) then appending a type declaration character will be needed to make the value come across correctly. For example this:
?&H9000,&H9000& will return this:
-28672 36864

I put the link to the relevant support article in "Homepage" link.

Try putting an ampersand at

Try putting an ampersand at the end, i.e. print &hff00&

Martin

vb6 hex fails

try print &hff00
you should get decimal 65280, but instead you get -256, which is wrong.
hex &hffffff00 is -256
what a load of pants

2 Bytes vs 4 Bytes

I believe what is happening is that because &hff00 is only 2 bytes long, VB assumes a smaller variable size (2 bytes instead of 4bytes) . You can 'trick'/force VB to do it by typing: print &h1ff00 - &h10000
in this case both numbers are >2 bytes forcing VB to use 4 byte calculations and then you do get 65280

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Allowed HTML tags: <a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd>
  • Lines and paragraphs break automatically.

More information about formatting options