|
-
Jul 26th, 2001, 10:33 PM
#1
Thread Starter
Dazed Member
Hex to Dec?
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.
-
Jul 27th, 2001, 12:50 AM
#2
Addicted Member
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
Code:
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
-
Jul 27th, 2001, 11:52 AM
#3
Thread Starter
Dazed Member
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...........
-
Jul 27th, 2001, 01:53 PM
#4
Addicted Member
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.
-
Jul 27th, 2001, 02:49 PM
#5
Thread Starter
Dazed Member
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........
-
Jul 27th, 2001, 03:06 PM
#6
Frenzied Member
Better coding techniques.
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.
Code:
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.
Code:
[(10*16 + 15)*16 + 3]*16 + 4
Note 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.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Jul 28th, 2001, 04:51 PM
#7
transcendental analytic
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.
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 28th, 2001, 10:26 PM
#8
Frenzied Member
From high to low.
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.
Live long & prosper.
The Dinosaur from prehistoric era prior to computers.
Eschew obfuscation!
If a billion people believe a foolish idea, it is still a foolish idea!
VB.net 2010 Express
64Bit & 32Bit Windows 7 & Windows XP. I run 4 operating systems on a single PC.
-
Jul 29th, 2001, 06:45 AM
#9
transcendental analytic
right. I was too tired to notice
Use  
writing software in C++ is like driving rivets into steel beam with a toothpick.
writing haskell makes your life easier:
reverse (p (6*9)) where p x|x==0=""|True=chr (48+z): p y where (y,z)=divMod x 13
To throw away OOP for low level languages is myopia, to keep OOP is hyperopia. To throw away OOP for a high level language is insight.
-
Jul 29th, 2001, 08:30 PM
#10
Registered User
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
VB Code:
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
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|