Results 1 to 10 of 10

Thread: Hex to Dec?

  1. #1

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418

    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.

  2. #2
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    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
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  3. #3

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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...........

  4. #4
    Addicted Member
    Join Date
    Jul 2000
    Location
    California
    Posts
    154
    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.
    VB-World addict!

    All spelling errors are undocumented words!
    http://www.bells.f2s.com

  5. #5

    Thread Starter
    Dazed Member
    Join Date
    Oct 1999
    Location
    Ridgefield Park, NJ
    Posts
    3,418
    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........

  6. #6
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    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.

  7. #7
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  8. #8
    Frenzied Member
    Join Date
    Jul 1999
    Location
    Huntingdon Valley, PA 19006
    Posts
    1,151

    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.

  9. #9
    transcendental analytic kedaman's Avatar
    Join Date
    Mar 2000
    Location
    0x002F2EA8
    Posts
    7,221
    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.

  10. #10
    Registered User Nucleus's Avatar
    Join Date
    Apr 2001
    Location
    So that's what you are up to ;)
    Posts
    2,530
    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:
    1. Function HexToDec(ByVal sHex As String) As Variant
    2.   Dim ba() As Byte, i&, bitval As Variant, b As Byte
    3.  bitval = 1
    4.  ba = sHex
    5.  For i = UBound(ba) - 1 To 0 Step -2
    6.     b = ba(i)
    7.     If b < 58 Then HexToDec = CDec(HexToDec + (b - 48) * bitval) Else HexToDec = CDec(HexToDec + (b - 55) * bitval)
    8.     bitval = CDec(bitval * 16)
    9.  Next i
    10. 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
  •  



Click Here to Expand Forum to Full Width