PDA

Click to See Complete Forum and Search --> : Hex to Dec?


Dillinger4
Jul 26th, 2001, 11:33 PM
Can any one pleaseeeeeee tell me how to convert hex to dec?
I understand but im not sure about an example i saw and i dont know how they are getting the end result.

0x5a = 5*16^1 + a*16^0 = 80 + 10 = 90

ok i understand but when i try to apply this example to this

0x7fffffff = 2147483647

i thought it would be.

7*16^7 + 15 *16^6 + 15*16^5 and down to zero, but i fail to come up with the correct result. Any Suggestions?

Thanks.

Bjwbell
Jul 27th, 2001, 01:50 AM
Thats the correct way of doing it; i came up with the right answer.
Try doing the math again.

Here's some code too do it for you


Option Explicit

Private Sub Command1_Click()
Dim s As String
Dim num As Double
Dim i As Integer
Dim temp As String

s = Text1.Text
s = LCase(s)
For i = 0 To Len(s) - 1
temp = Mid(s, Len(s) - i, 1)
Select Case temp
Case "1"
num = num + 1 * 16 ^ i
Case "2"
num = num + 2 * 16 ^ i
Case "3"
num = num + 3 * 16 ^ i
Case "4"
num = num + 4 * 16 ^ i
Case "5"
num = num + 5 * 16 ^ i

Case "6"
num = num + 6 * 16 ^ i
Case "7"
num = num + 7 * 16 ^ i
Case "8"
num = num + 8 * 16 ^ i
Case "9"
num = num + 9 * 16 ^ i
Case "a"
num = num + 10 * 16 ^ i
Case "b"
num = num + 11 * 16 ^ i
Case "c"
num = num + 12 * 16 ^ i
Case "d"
num = num + 13 * 16 ^ i
Case "e"

num = num + 14 * 16 ^ i
Case "f"
num = num + 15 * 16 ^ i
Case Else
MsgBox "Error"

End Select
Next
Text1.Text = num
End Sub


just put a button on the form and one text box

Dillinger4
Jul 27th, 2001, 12:52 PM
I see how you are using i to represent the index of say "f" within the string and you are using the index an an exponent. but i thought to calculate 0x7fffffff you have to f's dec value which is 15
ie. 15 *16^6 + 15*16^5...........

Bjwbell
Jul 27th, 2001, 02:53 PM
I find the value of F using the select case
when it finds an F, it executes the folowing code


Case "f"
num = num + 15 * 16 ^ i

where 15 is the decimal value of F.

Dillinger4
Jul 27th, 2001, 03:49 PM
Im sorry. I had another post right after my other one but it never posted. Maybe i hit preview instead of post. but any way..... yeah
i ran your code and using debug print the out put was.

15
255
4095
65535
1048575
16777215
268435455
2147483647

for 7fffffff. I guess i was just multipling wrong. Thanks........

Guv
Jul 27th, 2001, 04:06 PM
Your method looks okay. I too, got the correct answer on a hand calculator, so you are probably counting one too many or one too few hex characters.

I think the general approach provided by a previous post is not a good way to go.

I am too lazy to write a lot of code, but will supply some hints.

First. I would not use a case statement to translate the hex characters.Public Const HextString As String*16 = “0123456789abcdef”
. . . .
Dim Digit as Integer ‘Value of a Hex digit
Dim CharPosition as Integer ‘Assume this is position of Hex character in string
Dim HexString as String ‘Assume this is string containing hex characters’
Dim HexCharacter as String*1

HexCharacter = Mid(HexString, CharPosition, 1) - 1

Digit = Instr(HexString, HexCharacter)Next, integer radix conversion is exactly like evaluating a polynomial. When converting from Hex, you are evaluating a polynomial in X for X = 16, where the coefficients are the values of the Hex Digits. This is best done without evaluating high exponential values. For example, to convert AF34 to decimal, use a loop to evaluate the following algebraic expression.
[(10*16 + 15)*16 + 3]*16 + 4Note that the Hex digit A was implicitly multiplied by 16^3, without actually calculating 16^3.

BTW: When evaluating polynomials, it is essential to use the above technique. There are polynomials for which you can get imprecise results by calculating all the powers of the independent variable and adding the individual terms. This is especially true for polynomials like the series for sine, cosine, exponentials, et cetera.

kedaman
Jul 28th, 2001, 05:51 PM
vb has built in hex string evaluation:

dec=val("&h" & hex)

for oct you use "&o" instead of "&h"

for any other radix, guv's methods should be optimal.
Loop from lowest digit to highest digit, sum up and multiply by radix.

Guv
Jul 28th, 2001, 11:26 PM
When doing integer radix conversion and/or polynomial evaluation loop from high order to low order (left to right) not vice versa.

Start wiht highest order digit (or coefficient of highest order term) first.

kedaman
Jul 29th, 2001, 07:45 AM
right. I was too tired to notice

Nucleus
Jul 29th, 2001, 09:30 PM
If you need to convert large numbers from hex to decimal, VB's functions max out. Try this function if needed:

From hexidecimal to decimal, max number convertable approximately 33B2E3C9FD0803CE7FFFFFF

Function HexToDec(ByVal sHex As String) As Variant
Dim ba() As Byte, i&, bitval As Variant, b As Byte
bitval = 1
ba = sHex
For i = UBound(ba) - 1 To 0 Step -2
b = ba(i)
If b < 58 Then HexToDec = CDec(HexToDec + (b - 48) * bitval) Else HexToDec = CDec(HexToDec + (b - 55) * bitval)
bitval = CDec(bitval * 16)
Next i
End Function