Large Number Conversion

How do I convert a large number as the hexadecimal value &h80010047 to the decimal value 2147549255 ? I always get overflow and can't get it work correctly! Any help is greatly appreciated!

Thanks

DDuck

Comments

  • : How do I convert a large number as the hexadecimal value &h80010047 to the decimal value 2147549255 ? I always get overflow and can't get it work correctly! Any help is greatly appreciated!
    :
    : Thanks
    :
    : DDuck
    :

    Works fine for me, both directly and using Val(). What is the line you are having trouble with?
  • When using the below code line I get "-2147418041" as result, not 2147549255 as expected. Guess this must be an overflow... right? How do I do to avoid that? Im using VB6.

    msgBox Val(&h80010047) --> -2147418041 ????


    Thanks in advance.

    Lars



    : : How do I convert a large number as the hexadecimal value &h80010047 to the decimal value 2147549255 ? I always get overflow and can't get it work correctly! Any help is greatly appreciated!
    : :
    : : Thanks
    : :
    : : DDuck
    : :
    :
    : Works fine for me, both directly and using Val(). What is the line you are having trouble with?
    :

  • : When using the below code line I get "-2147418041" as result, not 2147549255 as expected. Guess this must be an overflow... right? How do I do to avoid that? Im using VB6.
    :

    Ah, ok, that's not an overflow. VB doesn't have unsigned integers so the 31st bit is reserved for a negative. I haven't tried it but you can try something like the following:

    [code]Dim x As Long
    Dim x1 As Double
    x = Val("&H80010047")
    If x < 0 Then
    x = x And Not 2 ^ 31
    x1 = CDbl(x) And 2 ^ 31 'or "And &H80000000"
    Else
    x1 = CDBl(x)
    End If
    [/code]
    That's really just a guess, you may have to adjust the code and it may not work at all.

    Hope this helps!
    KDL
  • Thanks for your support. Can't get it working thouh as it overflows as soon as I try to touch the ^ 31 bit. I wonder how they did it in the windows calculator.... Gee I wish I had that source :)

    Best Regards,

    L

    : : When using the below code line I get "-2147418041" as result, not 2147549255 as expected. Guess this must be an overflow... right? How do I do to avoid that? Im using VB6.
    : :
    :
    : Ah, ok, that's not an overflow. VB doesn't have unsigned integers so the 31st bit is reserved for a negative. I haven't tried it but you can try something like the following:
    :
    : [code]Dim x As Long
    : Dim x1 As Double
    : x = Val("&H80010047")
    : If x < 0 Then
    : x = x And Not 2 ^ 31
    : x1 = CDbl(x) And 2 ^ 31 'or "And &H80000000"
    : Else
    : x1 = CDBl(x)
    : End If
    : [/code]
    : That's really just a guess, you may have to adjust the code and it may not work at all.
    :
    : Hope this helps!
    : KDL
    :

  • : Thanks for your support. Can't get it working thouh as it overflows as soon as I try to touch the ^ 31 bit. I wonder how they did it in the windows calculator.... Gee I wish I had that source :)
    :

    More than likely, calc.exe "cheats". It can handle hundreds of digits of precision (decimals) without losing it's place so it's probably using a custom data type.

    May I ask what you need the large number for? There are other options. For example, you could use an array of bytes; with a little work, you could support numbers large enough to flood your RAM with data.
  • Best KD,

    Youre probably right about that its easy to get around it, unfournately I dont have the great knowledge of solving this issue. Can you give me a hint on what you mean with using the byte arrays? Do you know where I cand find any example of how to use that?

    My problem is that I get the hexadecimal value as a string and I can't really change the size of it. I need to convert it to human readable, i.e. decimal for presentation purposes.

    If you have any ide where I can find some example of how to convert this large number I would be more than grateful! Thanks again for all your great help my friend.

    // L

    : : Thanks for your support. Can't get it working thouh as it overflows as soon as I try to touch the ^ 31 bit. I wonder how they did it in the windows calculator.... Gee I wish I had that source :)
    : :
    :
    : More than likely, calc.exe "cheats". It can handle hundreds of digits of precision (decimals) without losing it's place so it's probably using a custom data type.
    :
    : May I ask what you need the large number for? There are other options. For example, you could use an array of bytes; with a little work, you could support numbers large enough to flood your RAM with data.
    :

  • : Youre probably right about that its easy to get around it, unfournately I dont have the great knowledge of solving this issue.
    :

    Now I never said it was easy! It probably is once you know exactly how to do it (most things are) but I don't know that it is.

    : Can you give me a hint on what you mean with using the byte arrays?

    Let's see. The idea is fairly simple to get started but it probably gets more complex depending on what all you want it to do. Here's a snippet that should help give you an idea:

    [code]Dim x As Integer 'Temp addition spot
    Dim y As Integer 'Carry spot
    Dim a(0 To 1) As Byte 'First number
    Dim b(0 To 1) As Byte 'Second number
    Dim c(0 To 2) As Byte 'Answer
    a(0) = 255: a(1) = 15 'a(1) being most sig. = 4095
    b(0) = 1: b(1) = 27 '= 6913

    'Now we add them:
    'First byte
    x = a(0) + b(0) '256.
    c(0) = x And 255 'Get least sig byte. c(0) = 0
    y = (x And 65280) / 256 'Get most sig byte. y = 1
    'Second byte, carry extra from first
    x = a(1) + b(1) + y 'x = 43
    c(1) = x And 255 'c(1) = 43
    y = (x And 65280) / 256 'y = 0
    'Third byte is just the carry since both
    'numbers (a and b) were only two bytes.
    c(2) = y 'c(2) = 0
    [/code]
    a was 4095. b was 6913. c is 11008.

    It's complex-looking but simple if you can understand what's going on. Anyway, it won't help with a hex to dec conversion. I simply can't think of a way to handle that...

    : Do you know where I cand find any example of how to use that?

    I'm afraid not, sorry.

    : My problem is that I get the hexadecimal value as a string and I can't really change the size of it. I need to convert it to human readable, i.e. decimal for presentation purposes.
    :
    : If you have any ide where I can find some example of how to convert this large number I would be more than grateful! Thanks again for all your great help my friend.
    :

    You can try a search on the net for GetDriveSize/GetFreeSpace or something like that. Those APIs use 8byte integers (uword, I believe) which VB can't normally handle. However, they show a method for getting the value into a Double, which does have enough space, even though the API don't support it. This may be your best bet, but I can't remember the name of the proper API.

    Sorry I couldn't help more,
    KDL
  • [code]
    Private Sub Form_Load()
    MsgBox Trim(Str(HConvVal("80010047")))
    End Sub

    Public Function HConvVal(HexStr As String) As Double
    Dim iHS As String, xHS As Integer, vHS As Integer, tHS As Double
    For xHS = Len(HexStr) To 1 Step -1
    iHS = Mid(HexStr, xHS, 1)
    vHS = Val("&H" + iHS + "&")
    tHS = tHS + vHS * 16 ^ (Len(HexStr) - xHS)
    Next
    HConvVal = tHS
    End Function
    [/code]

    : Best KD,
    :
    : Youre probably right about that its easy to get around it, unfournately I dont have the great knowledge of solving this issue. Can you give me a hint on what you mean with using the byte arrays? Do you know where I cand find any example of how to use that?
    :
    : My problem is that I get the hexadecimal value as a string and I can't really change the size of it. I need to convert it to human readable, i.e. decimal for presentation purposes.
    :
    : If you have any ide where I can find some example of how to convert this large number I would be more than grateful! Thanks again for all your great help my friend.
    :
    : // L
    :
    : : : Thanks for your support. Can't get it working thouh as it overflows as soon as I try to touch the ^ 31 bit. I wonder how they did it in the windows calculator.... Gee I wish I had that source :)
    : : :
    : :
    : : More than likely, calc.exe "cheats". It can handle hundreds of digits of precision (decimals) without losing it's place so it's probably using a custom data type.
    : :
    : : May I ask what you need the large number for? There are other options. For example, you could use an array of bytes; with a little work, you could support numbers large enough to flood your RAM with data.
    : :
    :
    :

  • THANKS!!!!!! Youre the greatest! I cant tell ya how happy these rows made me! No I got to try to understand it too, but that shouldn't be to hard :) THANKS!

    // Lars

    : [code]
    : Private Sub Form_Load()
    : MsgBox Trim(Str(HConvVal("80010047")))
    : End Sub
    :
    : Public Function HConvVal(HexStr As String) As Double
    : Dim iHS As String, xHS As Integer, vHS As Integer, tHS As Double
    : For xHS = Len(HexStr) To 1 Step -1
    : iHS = Mid(HexStr, xHS, 1)
    : vHS = Val("&H" + iHS + "&")
    : tHS = tHS + vHS * 16 ^ (Len(HexStr) - xHS)
    : Next
    : HConvVal = tHS
    : End Function
    : [/code]
    :
    : : Best KD,
    : :
    : : Youre probably right about that its easy to get around it, unfournately I dont have the great knowledge of solving this issue. Can you give me a hint on what you mean with using the byte arrays? Do you know where I cand find any example of how to use that?
    : :
    : : My problem is that I get the hexadecimal value as a string and I can't really change the size of it. I need to convert it to human readable, i.e. decimal for presentation purposes.
    : :
    : : If you have any ide where I can find some example of how to convert this large number I would be more than grateful! Thanks again for all your great help my friend.
    : :
    : : // L
    : :
    : : : : Thanks for your support. Can't get it working thouh as it overflows as soon as I try to touch the ^ 31 bit. I wonder how they did it in the windows calculator.... Gee I wish I had that source :)
    : : : :
    : : :
    : : : More than likely, calc.exe "cheats". It can handle hundreds of digits of precision (decimals) without losing it's place so it's probably using a custom data type.
    : : :
    : : : May I ask what you need the large number for? There are other options. For example, you could use an array of bytes; with a little work, you could support numbers large enough to flood your RAM with data.
    : : :
    : :
    : :
    :
    :

  • : [code]
    : Private Sub Form_Load()
    : MsgBox Trim(Str(HConvVal("80010047")))
    : End Sub
    :
    : Public Function HConvVal(HexStr As String) As Double
    : Dim iHS As String, xHS As Integer, vHS As Integer, tHS As Double
    : For xHS = Len(HexStr) To 1 Step -1
    : iHS = Mid(HexStr, xHS, 1)
    : vHS = Val("&H" + iHS + "&")
    : tHS = tHS + vHS * 16 ^ (Len(HexStr) - xHS)
    : Next
    : HConvVal = tHS
    : End Function
    : [/code]
    :

    Well, if you want to do it [italic]that[/italic] way...

    Good job, the walrus! That is so obvious now that I see it that I'm kind of surprised it never occured to me.
Sign In or Register to comment.

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories