hex to integer

usetheforce2
10-01-2001, 08:41 AM
hey all,

is there a function to converts a hex value to a decimal. Something like the hex() function that converts a number to a hex value. I know how to convert a decimal value long hand, and i could probably write a function to do this, but it might be to much over head, since i'll need to convert a lot of value back and forth.

thanks for any suggestions

"The crows seem to be calling his name, thought Caw." - Jack Handy

Yoda
10-01-2001, 08:51 AM
Er ... decimal to hex you want ?

Well ... <font color=blue>hex()</font color=blue> :D

<font color=green>Do or do not
There is no try</font color=green>

burningodzilla
10-01-2001, 08:59 AM
i asked the same question and got a great answer--but you have to know how VB sees values as hex; and i have forgotten. it's the value with something like "h&" as the prefix; anyway that's in the help files. assuming that the prefix is h&, you can append the hex value to this and then use Clng() to convert it.

Banjo
10-01-2001, 09:00 AM
Here's a pair of routines that convert from base 10 to any base between 2 and 36 and back again.
Function Base10toX(ByVal num As Long, ByVal lngBase As Long) As String
Dim i As Long, numDigits As Long
Dim b36 As String, digit As Long

If lngBase < 2 Or lngBase > 36 Then err.Raise vbObjectError + 1024, "Number Base Conversion", "Bases must be between 2 and 36"

' Calc the number of digits
numDigits = 1
Do Until Int(num / (lngBase ^ numDigits)) = 0
numDigits = numDigits + 1
Loop

For i = 1 To numDigits
digit = num Mod lngBase
num = Int(num / lngBase)

If digit >= 0 And digit <= 9 Then
b36 = digit & b36
ElseIf digit >= 10 And digit <= 35 Then
b36 = Chr((digit - 10) + 65) & b36
End If
Next i

Base10toX = b36
End Function

Function BaseXto10(ByVal num As String, ByVal lngBase As Long) As Long
Dim i As Integer, ch As String
Dim value As Long

num = StrReverse(UCase(num))
For i = 1 To Len(num)
ch = mid(num, i, 1)
If ch >= "A" And ch <= "Z" Then
value = value + (Asc(ch) - 65 + 10) * (lngBase ^ (i - 1))
ElseIf ch <= "0" And ch <= "9" Then
value = value + val(ch) * (lngBase ^ (i - 1))
End If
Next i

BaseXto10 = value
End Function

usetheforce2
10-01-2001, 09:00 AM
sorry may have worded it wrong: Example:

i can use, in vb, to convert 498 to a hex value:

hex(498) = 1F2

but to covernt back to decimal?

long hand:

"1F2"

2 X 16^0 = 2
F X 16^1 = 240
1 X 16^2 = 256

then add (2 + 240 + 256) = 498.

i was curious if vb has a function to do this, i can't seem to find it.

thanks again:





"The crows seem to be calling his name, thought Caw." - Jack Handy

burningodzilla
10-01-2001, 09:04 AM
here's the thread (http://www.visualbasicforum.com/bbs/showthreaded.php?Cat=&Board=visbas&Number=48039&page=&view=&sb=&o=)

----- Edit -----
Edit this msg to see what I did...it's also usually a good idea to make the thread flat before providing the link
<P ID="edit"><FONT class="small"><EM>Edited by KesleyK on 10/01/01 09:55 PM.</EM></FONT></P>

burningodzilla
10-01-2001, 09:11 AM
guess you didn't read my post images/icons/wink.gif!

you used this for hex:
hex(498) = 1F2
'OR
strHex=hex(498)
use this to get decimal back:
lngDec=Clng("&H"+strHex)

Mill
10-01-2001, 09:15 AM
To hide the "nasty stuff" just use <font color=blue>[.url=http://www.jsonline.com/packer/]Packer Plus Online[./url]</font color=blue> without the periods looks like this: Packer Plus Online (http://www.jsonline.com/packer/). Check the FAQ for the two ways of inserting links.


"The LORD is my strength and my shield" - Psalms 28:7

usetheforce2
10-01-2001, 09:49 AM
yes burningodzilla we all posted at the same time, 11:00 so i didn't see it until i posted.

thanks for your help!

Regan

"The crows seem to be calling his name, thought Caw." - Jack Handy

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum