Hex overflow

2»

Comments

  • Well if it is easy, then why not impress us w/ an example, because I've tried real hard to figure out a solution :)

    It has to be able to HEX a 50 digit decimal number.
  • I am going to look at the Hex function ( mentioned elsewhere here ) but i feel i may need to mess with maths using long strings.

    With the right code you can do maths on the numbers within strings of ANY length. I've done this ages ago to multiply two long numbers together. You are not limited to 32 digits with a 32 bit processor!!
    You could do 1024 bit maths or more ( provided your program can use 1024 character strings ).

    Even if this were the limit you can CARRY with your "STRING MATHS" to another string. Just think about the way you do maths on paper by hand and make a computer do the same. :-)

    I may tackle this again before Friday but in the mean time i need to do a WEB SITE project for my HND course on university as an in-course-assessment ( like homework ).

    My next approach may be to divide by the numbers which are powers of 16 like 16^0,16^1,16^2,16^3 etc, but in a reverse fashion, subtracting the number from the original number to be converted. I would start with>>

    16^x where x<=numberToBeConverted and is the highest power of 16.
    Feel free to start please....i need to get on with my web site. ;-)


    Regards,

    Dr M.

  • : Well if it is easy, then why not impress us w/ an example, because I've tried real hard to figure out a solution :)
    :
    : It has to be able to HEX a 50 digit decimal number.
    :

    OK, here's actually some asm I did. Becouse it was so easy...

    [code]

    xor bx,bx
    mov bx,data
    mov cl,4
    mov ah, 02h

    start:
    mov dh,[bx]
    mov dl,dh
    and dl,1111b
    mov di,dl
    mov dl,[hexnum+di]
    int 21h
    mov dl,dh
    shr dl,cl
    inc bx
    mov di,dl
    mov dl,[hexnum+di]
    int 21h

    cmp bx,endofData
    jb start

    mov ax,4C00h
    int 21h

    hexnum: db "0123456789ABCDEF"
    data: db 212,244,1,121
    endofData:
    [/code]

    I got some other funcs for string devide, but I can't find them.
  • It's beautiful, accept I do not understand a lick of assembly :(

    Now question is, can you translate it to VB?


    : : Well if it is easy, then why not impress us w/ an example, because I've tried real hard to figure out a solution :)
    : :
    : : It has to be able to HEX a 50 digit decimal number.
    : :
    :
    : OK, here's actually some asm I did. Becouse it was so easy...
    :
    : [code]
    :
    : xor bx,bx
    : mov bx,data
    : mov cl,4
    : mov ah, 02h
    :
    : start:
    : mov dh,[bx]
    : mov dl,dh
    : and dl,1111b
    : mov di,dl
    : mov dl,[hexnum+di]
    : int 21h
    : mov dl,dh
    : shr dl,cl
    : inc bx
    : mov di,dl
    : mov dl,[hexnum+di]
    : int 21h
    :
    : cmp bx,endofData
    : jb start
    :
    : mov ax,4C00h
    : int 21h
    :
    : hexnum: db "0123456789ABCDEF"
    : data: db 212,244,1,121
    : endofData:
    : [/code]
    :
    : I got some other funcs for string devide, but I can't find them.
    :

  • : With the right code you can do maths on the numbers within strings
    : of ANY length. I've done this ages ago to multiply two long numbers
    : together. You are not limited to 32 digits with a 32 bit processor!!
    : You could do 1024 bit maths or more ( provided your program can use
    : 1024 character strings ).

    Well I've done just that... Also needed hex with 16^15 values.

    Here is the code... a lot messy and names in portuguese/spanish. Hope it saves someone a lot of work :)

    [code]Public Function Myhex(entrada) As String
    Dim valor As String
    Dim c As Integer
    Dim resultado As String
    Dim vc As String
    Dim i As Integer
    valor = entrada
    For i = 16 To 0 Step -1
    vc = potencia(16, i)
    c = dividir(valor, vc)
    resultado = resultado & myhexchar(c)
    If c > 0 Then
    valor = subtrair(valor, multiplicar(vc, Trim(Val(c))))
    End If
    Next i
    While Left$(resultado, 1) = "0" And Len(resultado) > 1
    resultado = Mid$(resultado, 2, Len(resultado) - 1)
    Wend
    Myhex = resultado
    End Function
    Public Function myhexchar(ent) As String
    Dim res As String
    Select Case ent
    Case 0 To 9: res = Trim(Str(ent))
    Case 10 To 15: res = UCase(Chr$(ent + 87))
    End Select
    myhexchar = res
    End Function
    Function dividir(dividendo As String, divisor As String) As Integer
    Dim ress As Integer
    Dim mult As String
    ress = 0
    Do
    ress = ress + 1
    mult = multiplicar(divisor, Trim(Val(ress)))
    Loop While Not (maiorque(mult, dividendo))
    dividir = ress - 1
    End Function
    Function maiorque(x1 As String, x2 As String) As Boolean
    If Len(x1) > Len(x2) Then
    maiorque = True
    Exit Function
    End If

    If Len(x2) > Len(x1) Or x1 = x2 Then
    maiorque = False
    Exit Function
    End If

    For i = 1 To Len(x1)
    If Asc(Mid$(x1, i, 1)) < Asc(Mid$(x2, i, 1)) Then
    maiorque = False
    Exit Function
    ElseIf Asc(Mid$(x1, i, 1)) > Asc(Mid$(x2, i, 1)) Then
    maiorque = True
    Exit Function
    End If
    Next i

    maiorque = True

    End Function


    Function subtrair(maior As String, menor As String) As String
    Dim resultado As String
    Dim vienen As Integer
    Dim x, y As Integer
    Dim i As Integer
    vienen = 0
    For i = 1 To Len(maior)
    x = posicion(maior, i)
    y = posicion(menor, i) + vienen
    If y > x Then
    vienen = 1
    x = x + 10
    Else
    vienen = 0
    End If
    resultado = Trim(Val(x - y)) & resultado
    Next i
    While Left$(resultado, 1) = "0" And Len(resultado) > 1
    resultado = Mid$(resultado, 2, Len(resultado) - 1)
    Wend
    subtrair = resultado
    End Function
    Function potencia(base As Integer, expoente As Integer) As String
    Dim base_string As String
    Dim temp As String
    base_string = Trim(Str(base))
    temp = "1"
    If expoente = 0 Then GoTo final
    For i = 1 To expoente
    temp = multiplicar(temp, base_string)
    Next i
    final:
    potencia = temp
    End Function
    Function multiplicar(xxx As String, yyy As String) As String
    Dim a As String
    Dim b As String
    Dim c As String
    Dim linea(100) As String
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim tmp As Integer
    Dim vienen As Integer
    Dim resultado As String
    a = xxx
    b = yyy
    If Len(b) > Len(a) Then
    c = a
    a = b
    b = c
    End If
    For i = 1 To Len(b)
    linea(i) = String(i - 1, "0")
    vienen = 0
    For j = 1 To Len(a)
    tmp = posicion(b, i) * posicion(a, j) + vienen
    vienen = tmp 10
    linea(i) = Trim(Str(tmp Mod 10)) & linea(i)
    Next j
    If vienen > 0 Then linea(i) = Trim(Str(vienen)) & linea(i)
    Next i
    vienen = 0
    For i = 1 To Len(b) + Len(a)
    tmp = vienen
    For j = 1 To Len(a)
    tmp = tmp + posicion(linea(j), i)
    Next j
    vienen = tmp 10
    resultado = Trim(Str(tmp Mod 10)) & resultado
    Next i
    If Left$(resultado, 1) = "0" Then
    resultado = Mid$(resultado, 2, Len(resultado) - 1)
    End If
    multiplicar = resultado
    End Function
    Function posicion(z As String, x As Integer) As Integer
    If x > Len(z) Then
    posicion = 0
    Else
    posicion = Val(Mid$(z, Len(z) - x + 1, 1))
    End If
    End Function
    [/code]
  • : Well I've done just that... Also needed hex with 16^15 values.
    :
    : Here is the code... a lot messy and names in portuguese/spanish.
    : Hope it saves someone a lot of work :)
    :

    I glanced it over for a bit.

    Looks fairly ok to me, but it's a bit hard(er) to understand for us that don't speak spanish.
    Code-technically, it could (I think) use a couple of optimizations for speed but it looks like it'll work!
    So if you're just interested in doing a couple of calculations with bigger numbers than usual than it looks good.
    If you're however doing things like finding Prime Numbers of huge size then I think there is need for optimization.

    Just out of interest... this Thread is 14 months old. How did you ever come across it? :P

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
  • : Code-technically, it could (I think) use a couple of optimizations
    : for speed but it looks like it'll work!

    I would be happy to know you'r suggestions.

    : Just out of interest... this Thread is 14 months old. How did you
    : ever come across it? :P

    I needed a 16^15 hex conversor in VB6. This thread was the 3rd result in googling "vb6 hex overflow". I though it could help someone with the same problem.

    : Looks fairly ok to me, but it's a bit hard(er) to understand for us
    : that don't speak spanish.

    Here's a "translation".I think it's right (Find & Replace)... Haven't test it.

    [code]Public Function Myhex(input_value) As String
    Dim value As String
    Dim c As Integer
    Dim result As String
    Dim vc As String
    Dim i As Integer
    value = input_value
    For i = 16 To 0 Step -1
    vc = my_power(16, i)
    c = divide(value, vc)
    result = result & myhexchar(c)
    If c > 0 Then
    value = subtract(value, multiply(vc, Trim(Val(c))))
    End If
    Next i
    While Left$(result, 1) = "0" And Len(result) > 1
    result = Mid$(result, 2, Len(result) - 1)
    Wend
    Myhex = result
    End Function
    Public Function myhexchar(ent) As String
    Dim res As String
    Select Case ent
    Case 0 To 9: res = Trim(Str(ent))
    Case 10 To 15: res = UCase(Chr$(ent + 87))
    End Select
    myhexchar = res
    End Function
    Function divide(dividend As String, divisor As String) As Integer
    Dim ress As Integer
    Dim mult As String
    ress = 0
    Do
    ress = ress + 1
    mult = multiply(divisor, Trim(Val(ress)))
    Loop While Not (greaterthan(mult, dividend))
    divide = ress - 1
    End Function
    Function greaterthan(x1 As String, x2 As String) As Boolean
    If Len(x1) > Len(x2) Then
    greaterthan = True
    Exit Function
    End If

    If Len(x2) > Len(x1) Or x1 = x2 Then
    greaterthan = False
    Exit Function
    End If

    For i = 1 To Len(x1)
    If Asc(Mid$(x1, i, 1)) < Asc(Mid$(x2, i, 1)) Then
    greaterthan = False
    Exit Function
    ElseIf Asc(Mid$(x1, i, 1)) > Asc(Mid$(x2, i, 1)) Then
    greaterthan = True
    Exit Function
    End If
    Next i

    greaterthan = True

    End Function


    Function subtract(minuend As String, subtrahend As String) As String
    Dim result As String
    Dim carried As Integer
    Dim x, y As Integer
    Dim i As Integer
    carried = 0
    For i = 1 To Len(minuend)
    x = position(minuend, i)
    y = position(subtrahend, i) + carried
    If y > x Then
    carried = 1
    x = x + 10
    Else
    carried = 0
    End If
    result = Trim(Val(x - y)) & result
    Next i
    While Left$(result, 1) = "0" And Len(result) > 1
    result = Mid$(result, 2, Len(result) - 1)
    Wend
    subtract = result
    End Function
    Function my_power(base As Integer, exponent As Integer) As String
    Dim base_string As String
    Dim temp As String
    base_string = Trim(Str(base))
    temp = "1"
    If exponent = 0 Then GoTo final
    For i = 1 To exponent
    temp = multiply(temp, base_string)
    Next i
    final:
    my_power = temp
    End Function
    Function multiply(xxx As String, yyy As String) As String
    Dim a As String
    Dim b As String
    Dim c As String
    Dim line(100) As String
    Dim i As Integer
    Dim j As Integer
    Dim k As Integer
    Dim tmp As Integer
    Dim carried As Integer
    Dim result As String
    a = xxx
    b = yyy
    If Len(b) > Len(a) Then
    c = a
    a = b
    b = c
    End If
    For i = 1 To Len(b)
    line(i) = String(i - 1, "0")
    carried = 0
    For j = 1 To Len(a)
    tmp = position(b, i) * position(a, j) + carried
    carried = tmp 10
    line(i) = Trim(Str(tmp Mod 10)) & line(i)
    Next j
    If carried > 0 Then line(i) = Trim(Str(carried)) & line(i)
    Next i
    carried = 0
    For i = 1 To Len(b) + Len(a)
    tmp = carried
    For j = 1 To Len(a)
    tmp = tmp + position(line(j), i)
    Next j
    carried = tmp 10
    result = Trim(Str(tmp Mod 10)) & result
    Next i
    If Left$(result, 1) = "0" Then
    result = Mid$(result, 2, Len(result) - 1)
    End If
    multiply = result
    End Function
    Function position(z As String, x As Integer) As Integer
    If x > Len(z) Then
    position = 0
    Else
    position = Val(Mid$(z, Len(z) - x + 1, 1))
    End If
    End Function[/code]
  • : : Code-technically, it could (I think) use a couple of optimizations
    : : for speed but it looks like it'll work!
    :
    : I would be happy to know you'r suggestions.
    :

    Well for values up to 16^15 it should have quite good peformance (relatively speaking).
    I am currently also developing a Big Number library, only then for ridiculously big numbers. However, I'm not really making it from a practical point of view, but more for fun and to challenge myself to get as optimized code as possible for VERY BIG numbers :) The code I have written should perform quite well when the numbers are 10^20 +. But like I said: there is probably very little practical need to do such a thing ;)

    I'd share some of my thoughts with you, but I can't yet be sure of the code. I haven't thoroughly tested it yet.

    I might make another reply with some thoughts I had, but I don't think your code needs any of it. It's quite good.

    :
    : I needed a 16^15 hex conversor in VB6. This thread was the 3rd
    : result in googling "vb6 hex overflow". I though it could help
    : someone with the same problem.
    :

    Yeah sure :) Usually, bumping is a bad thing, but in this case I think it's quite ok to do so.

    :
    : Here's a "translation".I think it's right (Find & Replace)...
    : Haven't test it.
    :

    Ahh helps :) Makes the code faster to read

    Best Regards,
    Richard

    The way I see it... Well, it's all pretty blurry
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