Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

dec to hex & oct 2

Status
Not open for further replies.

geeteshk

Programmer
Aug 19, 2002
58
IN
how do i convert
a decimal number to octal and hexadecimal
in VB using some function
 
how do you convert back to decimal from hex ?

i don't believe there is a Dec function...
 
got it figured out...

use cdec("&H" & vartoconvertodecimal)

thanx anyways
 
Er, but you don't have to do this. Both the Oct and Hex methods return a string representation of the source decimal number. VB continues to work in decimal. To get the decimal of an octal or hex string representation you just need to do:

Val(<oct_or_hex_string>)

CDec does something slightly different
 
strongm, I think &quot;&H&quot; must be provided. Otherwise VB does not know whether the source number is decimal, hexadecimal or octal. Consider the following example.
___

Dim HexNum As String, DecNum As Long

DecNum = 60

HexNum = Hex$(DecNum)
MsgBox HexNum 'Says 3C

DecNum = Val(HexNum)
MsgBox DecNum 'Says 3 ignoring C (wrong)

DecNum = Val(&quot;&H&quot; & HexNum)
MsgBox DecNum 'Says 60 (correct)
___

For me, the simplest way to convert a hex or oct number to its decimal is this;
___

Dim HexNum As String, OctNum As String, DecNum As Long
HexNum = &quot;EC&quot;
DecNum = &quot;&H&quot; & HexNum
MsgBox DecNum '236

'Similarly,
OctNum = &quot;46&quot;
DecNum = &quot;&O&quot; & OctNum
MsgBox DecNum '38
 
Yes, correct. That's why I described them as <oct_or_hex_string> rather than just <string>. I probably wasn't clear enough
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top