User Tools

Site Tools


32 bit hexadecimal to decimal conversion

By Graham.

In cases you need to print decimal values, you can use this routine to convert any unsigned 32 bit value to decimal. It doesn't use the BCD mode. The conversion is done by repeatedly dividing the 32 bit value by 10 and storing the remainder of each division as decimal digits.

        ; prints a 32 bit value to the screen
        jsr hex2dec

        ldx #9
l1      lda result,x
        bne l2
        dex             ; skip leading zeros
        bne l1

l2      lda result,x
        ora #$30
        jsr $ffd2
        bpl l2

        ; converts 10 digits (32 bit values have max. 10 decimal digits)
        ldx #0
l3      jsr div10
        sta result,x
        cpx #10
        bne l3

        ; divides a 32 bit value by 10
        ; remainder is returned in akku
        ldy #32         ; 32 bits
        lda #0
l4      rol
        cmp #10
        bcc skip
        sbc #10
skip    rol value
        rol value+1
        rol value+2
        rol value+3
        bpl l4

value   .byte $ff,$ff,$ff,$ff

result  .byte 0,0,0,0,0,0,0,0,0,0

16-bit version with tightened up loops and routine structure:

    // Converts 16 bit values to 5 decimal digits.
    ldx #-5
    ldy #16                    // Divides a 16 bit value by 10 (Remainder in A)
    lda #0
!:  rol
    cmp #10
    bcc !+
        sbc #10
!:  rol value
    rol value+1
    bpl !--
    sta result-[255-4],x
    bne !Loop-
    // Print result, skip leading zeros
    ldx #4
!:  lda result,x
    beq !Skip+
    ora #$30
    jsr $ffd2
    bpl !-

value:  .word 65535
result: .byte 0,0,0,0,0

NB! If you're only looking to convert, not print; Replace jsr $ffd2 with sta result,x.

NB!! If you want to maintain digit positioning, replacing proceeding zero's with #$20.

NB!!! If you wish to extend to 24, 32 or whatever size, adjust the following values:

    ldx #-5               // How many digits as a negative number
    ldy #16               // How many bits as a positive number
    sta result-[255-4],x  // Change the "-4" to the negative quantity of digits-1 (i.e. 24 bit => [255-23]
    ldx #4                // How many digits-1 as a positive number
value:  .word 65535       // Adjust to quantity of bits needed
result: .byte 0,0,0,0,0   // Adjust to quantity of digits needed
Finally adjust the quantity of "rol value+n" to: roundup(bits/8)
base/32_bit_hexadecimal_to_decimal_conversion.txt · Last modified: 2023-04-20 09:02 by tww