Howdy, Stranger!

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

Categories

Converting Decimal numbers to Hex

NavicNavic Member Posts: 109
I just don't get it!! I've read so much about converting numbers and all I understand is how to convert Hex to Decimal, but I want to convert a Decimal number to Hex, can anyone explain how to? Examples would be great too.

Thanks,
Navic

Comments

  • blipblip Member Posts: 756
    Multiply your destination by 10, add a decimal digit (if it's ASCII, subtract 30h from it first), and repeat until you're done.
  • NavicNavic Member Posts: 109
    : Multiply your destination by 10, add a decimal digit (if it's ASCII, subtract 30h from it first), and repeat until you're done.
    :


    What do you mean destination? If I had the decimal number 933, how would I get the hex value of 3A5h?
  • blipblip Member Posts: 756
    In this example I will use the 32-bit version of MUL:
    [code]DecASCII2Hex PROC
    mov si,offset ASCIInum
    xor ecx,ecx
    xor eax,eax
    xor edx,edx
    mov ebp,10 ;The source base is decimal.

    Dec2HexLoop:
    mov cl,[si]
    test cl,cl ;Check to see if this byte is zero.
    jz OutOfHere
    mul ebp ;EDX:EAX = EAX * EBP
    jc OverFlow ;Check for an unsigned overflow.
    sub cl,30h ;Convert the ASCII digit to BCD.
    inc si ;Increment the pointer.
    add eax,ecx ;Add the BCD digit to the total.
    jnc Dec2HexLoop
    OverFlow: ;CF is already set if here.
    OutOfHere: ;CF is already clear here.
    ;At this point, EAX should be equal to 3A5h.
    ret
    DecASCII2Hex ENDP

    ASCIInum db "933",0 ;Most decimal numbers come in in ASCII form.[/code]

    In the example, the destination was EAX (I also called it the total).
  • Shawn CarterShawn Carter Member Posts: 0

    // http://forcoder.org // free video tutorials and ebooks about | Visual Basic, MATLAB, Visual Basic .NET, C, Delphi, JavaScript, Assembly, Objective-C, C#, Python, R, Ruby, PL/SQL, Go, Scratch, C++, Java, Swift, Perl, PHP Hack, Scheme, Ada, Bash, Rust, Fortran, Awk, Kotlin, Prolog, F#, Logo, Erlang, ML, ABAP, Alice, Crystal, Julia, Dart, SAS, FoxPro, Clojure, VBScript, Lua, D, Scala, LabVIEW, Apex, COBOL, Transact-SQL, Lisp | _____________

Sign In or Register to comment.