Discussion:
[PIC] Hex to Decimal routines
Dwayne Reid
2018-01-17 18:04:31 UTC
Permalink
Good day to all.

I'm looking for an efficient method of converting a hex number to
decimal. I'm aware of and have used both Scott Dattalo's 8-bit to
Decimal routine as well as John Payson't 16-bit to Decimal routine
but I'm looking for something in-between those two.

I'm starting off with a 12-bit number but eventually wind up with a
9-bit result after scaling. Ideally, I'd like two optimized
routines: 9-bit to Decimal and 12-bit to Decimal.

Any ideas?

Many thanks!

dwayne
--
Dwayne Reid <***@planet.eon.net>
Trinity Electronics Systems Ltd Edmonton, AB, CANADA
780-489-3199 voice 780-487-6397 fax 888-489-3199 Toll Free
www.trinity-electronics.com
Custom Electronics Design and Manufacturing
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Jason White
2018-01-17 19:30:25 UTC
Permalink
Out of curiosity which processor family are you using?
Post by Dwayne Reid
Good day to all.
I'm looking for an efficient method of converting a hex number to
decimal. I'm aware of and have used both Scott Dattalo's 8-bit to
Decimal routine as well as John Payson't 16-bit to Decimal routine
but I'm looking for something in-between those two.
I'm starting off with a 12-bit number but eventually wind up with a
9-bit result after scaling. Ideally, I'd like two optimized
routines: 9-bit to Decimal and 12-bit to Decimal.
Any ideas?
Many thanks!
dwayne
--
Trinity Electronics Systems Ltd Edmonton, AB, CANADA
780-489-3199 voice 780-487-6397 fax 888-489-3199 Toll Free
www.trinity-electronics.com
Custom Electronics Design and Manufacturing
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
--
Jason White
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Forrest Christian (List Account)
2018-01-17 20:10:38 UTC
Permalink
Do you mean a hex string or an integer? And, Do you mean a string for
output, or bcd?

And is this for output to a display, or to a serial port, etc., or internal
storage.

Also, are you wanting a solution which is optimized for code space or
execution speed?

What language and processor?
Post by Dwayne Reid
Good day to all.
I'm looking for an efficient method of converting a hex number to
decimal. I'm aware of and have used both Scott Dattalo's 8-bit to
Decimal routine as well as John Payson't 16-bit to Decimal routine
but I'm looking for something in-between those two.
I'm starting off with a 12-bit number but eventually wind up with a
9-bit result after scaling. Ideally, I'd like two optimized
routines: 9-bit to Decimal and 12-bit to Decimal.
Any ideas?
Many thanks!
dwayne
--
Trinity Electronics Systems Ltd Edmonton, AB, CANADA
780-489-3199 voice 780-487-6397 fax 888-489-3199 Toll Free
www.trinity-electronics.com
Custom Electronics Design and Manufacturing
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Isaac M. Bavaresco
2018-01-17 20:54:31 UTC
Permalink
Perhaps this is not what you need, but check my code below.

It is an improved version of the routines from Microchip's C18 compiler,
for PIC18F extended architecture, reentrant.

Implement routines itoa and utoa in assembly.

Cheers,

Isaac


;===============================================================================
; Copyright (c) 2008, Isaac Marino Bavaresco
; All rights reserved
; ***@yahoo.com.br
;===============================================================================
#include "P18CXXX.INC"
;===============================================================================
        radix    decimal
;===============================================================================
STDLIB        code
;===============================================================================
s        equ    0
v        equ    2
Aux        equ    6

#define    NotZero    0
;===============================================================================
;  char *utoa( auto unsigned short v, auto char RAM *s );

        global    utoa

utoa:        bsf    STATUS,C,ACCESS        ; Unsigned = 1;
        bra    Common
;===============================================================================
;  char *itoa( auto signed short v, auto char RAM *s );

        global    itoa

itoa:        bcf    STATUS,C,ACCESS        ; Unsigned = 0;
;===============================================================================
Common:        movff    FSR2L,POSTINC1        ; *(WORD*)FSR1++ = FSR2;
        movff    FSR2H,POSTINC1

        movff    FSR1L,FSR2L        ; FSR2         = FSR1
        movff    FSR1H,FSR2H

        subfsr    2,6            ; FSR2        -= 4;
        addfsr    1,3
        ;---------------------------------------------------------------
        movf    [s+0],w            ; PROD = FSR0     = s;
        movwf    FSR0L,ACCESS
        movwf    PRODL,ACCESS
        movf    [s+1],w
        movwf    FSR0H,ACCESS
        movwf    PRODH,ACCESS
        ;---------------------------------------------------------------
        btfss    STATUS,C,ACCESS        ; if( !Unsigned && v < 0 )
        btfss    [v+1],7            ;     {
        bra    Positive
       
Negative:    movlw    '-'            ;     *s++ = '-'
        movwf    POSTINC0,ACCESS

        subwf    WREG,f,ACCESS        ;     v = -v; }
        subfwb    [v+0],f
        subfwb    [v+1],f
        ;
        ;
        ;---------------------------------------------------------------
Positive:    clrf    [Aux+0]
        clrf    [Aux+1]
        clrf    [Aux+2]
        ;
        ;

        movlw    15
        movwf    TBLPTRL,ACCESS

        ;----------------------

LessThanEight:    rlcf    [v+0],f                ;  3
        rlcf    [v+1],f                ;  4
        ;
        ;
        ;
        ;
        rlcf    [Aux+2],f            ;  9
        rlcf    [Aux+1],f            ; 10
        rlcf    [Aux+0],f            ; 11

        btfsc    [Aux+2],2
        bra    Loop

        decfsz    TBLPTRL,f,ACCESS        ; 12
        bra    LessThanEight            ; 13/14

        ;----------------------

LoopStart:    incf    TBLPTRL,f,ACCESS

Loop:   movlw    3                ;  1
        movwf    TBLPTRH,ACCESS        ;  0

DigitLoop:    movlw    0x03            ;  1
        addwf    [Aux+0],w        ;  2
        btfsc    WREG,3,ACCESS        ;  3
        movwf    [Aux+0]            ;  4
        movlw    0x30            ;  5
        addwf    [Aux+0],w        ;  6
        btfsc    WREG,7,ACCESS        ;  7
        movwf    [Aux+0]            ;  8

        addfsr    2,1            ;  9
        decfsz    TBLPTRH,f,ACCESS    ; 10
        bra    DigitLoop        ; 11/12
        subfsr    2,3                ;  2

        rlcf    [v+0],f                ;  3
        rlcf    [v+1],f                ;  4
        ;
        ;
        ;
        ;
        rlcf    [Aux+2],f            ;  9
        rlcf    [Aux+1],f            ; 10
        rlcf    [Aux+0],f            ; 11

        decfsz    TBLPTRL,f,ACCESS        ; 12
        bra    Loop                ; 13/14
        ;---------------------------------------------------------------
        bcf    TBLPTRH,NotZero,ACCESS
        movlw    3
        movwf    TBLPTRL            ;  0

Tens:   swapf    [Aux+0],w        ;  1
        andlw    0x0f            ;  2
        btfss    TBLPTRH,NotZero,ACCESS    ;  3
        bz    Units            ;  4
        bsf    TBLPTRH,NotZero,ACCESS    ;  5
        addlw    '0'            ;  6
        movwf    POSTINC0,ACCESS        ;  7

Units:  movf    [Aux+0],w        ;  8
        andlw    0x0f            ;  9
        btfss    TBLPTRH,NotZero,ACCESS    ; 10
        bz    Next            ; 11
        bsf    TBLPTRH,NotZero,ACCESS    ; 12
        addlw    '0'            ; 13
        movwf    POSTINC0,ACCESS        ; 14
       
Next:   addfsr    2,1            ; 15
        decfsz    TBLPTRL,f,ACCESS    ; 16
        bra    Tens            ; 17/18

        movlw    '0'
        btfss    TBLPTRH,NotZero,ACCESS
        movwf    POSTINC0,ACCESS

        clrf    INDF0,ACCESS
        ;---------------------------------------------------------------
Epilog: subfsr    1,4
        movff    POSTDEC1,FSR2H
        movff    INDF1,FSR2L
        return    0
;===============================================================================
        end
;===============================================================================
Post by Dwayne Reid
Good day to all.
I'm looking for an efficient method of converting a hex number to
decimal. I'm aware of and have used both Scott Dattalo's 8-bit to
Decimal routine as well as John Payson't 16-bit to Decimal routine
but I'm looking for something in-between those two.
I'm starting off with a 12-bit number but eventually wind up with a
9-bit result after scaling. Ideally, I'd like two optimized
routines: 9-bit to Decimal and 12-bit to Decimal.
Any ideas?
Many thanks!
dwayne
---
Este email foi escaneado pelo Avast antivírus.
https://www.avast.com/antivirus
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.
Dwayne Reid
2018-01-17 21:13:18 UTC
Permalink
Post by Forrest Christian (List Account)
Do you mean a hex string or an integer? And, Do you mean a string for
output, or bcd?
Hex Integer. More than one byte wide, less than two full bytes wide.
Post by Forrest Christian (List Account)
And is this for output to a display, or to a serial port, etc., or internal
storage.
Will be used for driving a 3-digit 7-segment display.
Post by Forrest Christian (List Account)
Also, are you wanting a solution which is optimized for code space or
execution speed?
Both <grin>.

Scott Dattalo's 8-bit to Decimal routine (bin2bcd) operates in 28 cycles.
Joun Payson's 16-bit to Decimal routine is longer (don't recall the
longest time).
Post by Forrest Christian (List Account)
What language and processor?
Good point. PIC16 family, assembler


I'll include (below) the two routines that I mentioned above.

;Bin2bcd
;convert 8 bit hex # to 3 digit decimal (0..255)
;Scott Dattalo 28 cycles
;
movwf bin
clrf hundreds
swapf bin,W ;add upper & lower nybbles to get 1's digit
addwf bin,W
andlw 0x0F

skpndc ;1's digit binary to bcd conversion
addlw 0x16
skpndc
addlw 0x06
addlw 0x06
skpdc
addlw -0x06

btfsc bin,4 ;bit 4 is special case
addlw 0x16 - 1 + 0x6
skpdc
addlw -0x06

btfsc bin,5 ;2^5 =32 so add 3 to 10's digit if b5==1
addlw 0x30

btfsc bin,6 ;2^6 =64 so add 6 to 10's digit if b6==1
addlw 0x60

btfsc bin,7 ;2^7 =128 so add 2 to 10's digit if b7==1
addlw 0x20

; addlw 0x60 ;convert 10's digit to BCD
; rlf hundreds,F ;propagate C into 100's
; btfss hundreds,0 ;was there in fact a carry?
; addlw -0x60 ;nope - undo conversion
;
; btfsc bin,7 ;input > .200?
; incf hundreds,F ;yep - inc hundreds
;
; movwf bin ;10's & 1's packed BCD result

;use either commented out section above or this below
addlw 0x60 ;convert 10's digit to BCD
skpnc ;overflow?
movlw 0x99
skpc
addlw -0x60 ;

movwf bin ;10's & 1's packed BCD result
return


;;Takes hex number in NumH:NumL Returns decimal in TenK:Thou:Hund:Tens:Ones
;;written by John Payson
TenK EQU LOOPER ;share variables
Thou EQU D2
Hund EQU D1
Tens EQU R2
Ones EQU R1
swapf NumH,w
iorlw b'11110000'
movwf Thou
addwf Thou,f
addlw .226
movwf Hund
addlw .50
movwf Ones

movfw NumH
andlw b'00001111'
addwf Hund,f
addwf Hund,f
addwf Ones,f
addlw .233
movwf Tens
addwf Tens,f
addwf Tens,f

swapf NumL,w
andlw b'00001111'
addwf Tens,f
addwf Ones,f

rlf Tens,f
rlf Ones,f
comf Ones,f
rlf Ones,f

movfw NumL
andlw b'00001111'
addwf Ones,f
rlf Thou,f

movlw 7
movwf TenK

; At this point, the original # == TenK*10000+Thou*1000+Hund*100+Tens*10+Ones
; if those entities are regarded as two's compliment binary. To be precise,
; all of them are negative except TenK. Now the number needs to be normalized,
; but this can all be done with simple byte arithmetic.

movlw .10
Lb1:
addwf Ones,f
decf Tens,f
skpc
goto Lb1
Lb2:
addwf Tens,f
decf Hund,f
skpc
goto Lb2
Lb3:
addwf Hund,f
decf Thou,f
skpc
goto Lb3
Lb4:
addwf Thou,f
decf TenK,f
skpc
goto Lb4
return
Post by Forrest Christian (List Account)
Post by Dwayne Reid
Good day to all.
I'm looking for an efficient method of converting a hex number to
decimal. I'm aware of and have used both Scott Dattalo's 8-bit to
Decimal routine as well as John Payson't 16-bit to Decimal routine
but I'm looking for something in-between those two.
I'm starting off with a 12-bit number but eventually wind up with a
9-bit result after scaling. Ideally, I'd like two optimized
routines: 9-bit to Decimal and 12-bit to Decimal.
Any ideas?
Many thanks!
dwayne
--
Dwayne Reid <***@planet.eon.net>
Trinity Electronics Systems Ltd Edmonton, AB, CANADA
780-489-3199 voice 780-487-6397 fax 888-489-3199 Toll Free
www.trinity-electronics.com
Custom Electronics Design and Manufacturing
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Jan-Erik Söderholm
2018-01-17 22:15:19 UTC
Permalink
Hi.
Post by Dwayne Reid
Hex Integer. More than one byte wide, less than two full bytes wide.
What is an "Hex integer"? Is it a binary value? Somewhere between 8 and 16
bits not counting leading zeroes? Or is it a value expressed as hexadecimal
string?

Note that "8F" is not the same as 0x8F. The first has the dual byte binary
value
'0011 1000 0100 0110' (the two characters '8' and 'F' in ASCII encoding).
The second is the single byte binary value '10001111' (but expressed in
hex).

Saying that a PIC processor has something called a "hex value" is not
particular meaningful.

And the conversion is very different if it is from a hex string (in ASCII)
or
if it is really from a binary value.

I *think* that you meant to say a "binary integer".
Post by Dwayne Reid
More than one byte wide...
Does that imply there are no values below 0xFF?
Doesn't matter maybe...

Regards, Jan-Erik.




-----Ursprungligt meddelande-----
Från: piclist-***@mit.edu [mailto:piclist-***@mit.edu] För Dwayne
Reid
Skickat: den 17 januari 2018 22:13
Till: Microcontroller discussion list - Public. <***@mit.edu>
Ämne: Re: [PIC] Hex to Decimal routines
Post by Dwayne Reid
Do you mean a hex string or an integer? And, Do you mean a string for
output, or bcd?
Hex Integer. More than one byte wide, less than two full bytes wide.
Post by Dwayne Reid
And is this for output to a display, or to a serial port, etc., or
internal storage.
Will be used for driving a 3-digit 7-segment display.
Post by Dwayne Reid
Also, are you wanting a solution which is optimized for code space or
execution speed?
Both <grin>.

Scott Dattalo's 8-bit to Decimal routine (bin2bcd) operates in 28 cycles.
Joun Payson's 16-bit to Decimal routine is longer (don't recall the longest
time).
Post by Dwayne Reid
What language and processor?
Good point. PIC16 family, assembler


I'll include (below) the two routines that I mentioned above.

;Bin2bcd
;convert 8 bit hex # to 3 digit decimal (0..255) ;Scott Dattalo 28 cycles ;
movwf bin
clrf hundreds
swapf bin,W ;add upper & lower nybbles to get 1's digit
addwf bin,W
andlw 0x0F

skpndc ;1's digit binary to bcd conversion
addlw 0x16
skpndc
addlw 0x06
addlw 0x06
skpdc
addlw -0x06

btfsc bin,4 ;bit 4 is special case
addlw 0x16 - 1 + 0x6
skpdc
addlw -0x06

btfsc bin,5 ;2^5 =32 so add 3 to 10's digit if b5==1
addlw 0x30

btfsc bin,6 ;2^6 =64 so add 6 to 10's digit if b6==1
addlw 0x60

btfsc bin,7 ;2^7 =128 so add 2 to 10's digit if b7==1
addlw 0x20

; addlw 0x60 ;convert 10's digit to BCD
; rlf hundreds,F ;propagate C into 100's
; btfss hundreds,0 ;was there in fact a carry?
; addlw -0x60 ;nope - undo conversion
;
; btfsc bin,7 ;input > .200?
; incf hundreds,F ;yep - inc hundreds
;
; movwf bin ;10's & 1's packed BCD result

;use either commented out section above or this below
addlw 0x60 ;convert 10's digit to BCD
skpnc ;overflow?
movlw 0x99
skpc
addlw -0x60 ;

movwf bin ;10's & 1's packed BCD result
return


;;Takes hex number in NumH:NumL Returns decimal in TenK:Thou:Hund:Tens:Ones
;;written by John Payson
TenK EQU LOOPER ;share variables
Thou EQU D2
Hund EQU D1
Tens EQU R2
Ones EQU R1
swapf NumH,w
iorlw b'11110000'
movwf Thou
addwf Thou,f
addlw .226
movwf Hund
addlw .50
movwf Ones

movfw NumH
andlw b'00001111'
addwf Hund,f
addwf Hund,f
addwf Ones,f
addlw .233
movwf Tens
addwf Tens,f
addwf Tens,f

swapf NumL,w
andlw b'00001111'
addwf Tens,f
addwf Ones,f

rlf Tens,f
rlf Ones,f
comf Ones,f
rlf Ones,f

movfw NumL
andlw b'00001111'
addwf Ones,f
rlf Thou,f

movlw 7
movwf TenK

; At this point, the original # ==
TenK*10000+Thou*1000+Hund*100+Tens*10+Ones
; if those entities are regarded as two's compliment binary. To be precise,
; all of them are negative except TenK. Now the number needs to be
normalized, ; but this can all be done with simple byte arithmetic.

movlw .10
Lb1:
addwf Ones,f
decf Tens,f
skpc
goto Lb1
Lb2:
addwf Tens,f
decf Hund,f
skpc
goto Lb2
Lb3:
addwf Hund,f
decf Thou,f
skpc
goto Lb3
Lb4:
addwf Thou,f
decf TenK,f
skpc
goto Lb4
return
Post by Dwayne Reid
Post by Dwayne Reid
Good day to all.
I'm looking for an efficient method of converting a hex number to
decimal. I'm aware of and have used both Scott Dattalo's 8-bit to
Decimal routine as well as John Payson't 16-bit to Decimal routine
but I'm looking for something in-between those two.
I'm starting off with a 12-bit number but eventually wind up with a
9-bit result after scaling. Ideally, I'd like two optimized
routines: 9-bit to Decimal and 12-bit to Decimal.
Any ideas?
Many thanks!
dwayne
--
Dwayne Reid <***@planet.eon.net>
Trinity Electronics Systems Ltd Edmonton, AB, CANADA
780-489-3199 voice 780-487-6397 fax 888-489-3199 Toll Free
www.trinity-electronics.com
Custom Electronics Design and Manufacturing

--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change
your membership options at http://mailman.mit.edu/mailman/listinfo/piclist
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Dwayne Reid
2018-01-17 23:32:13 UTC
Permalink
Hi there, Jan-Erik.

I thought that my original question was
reasonably clear: I specifically mention that I'm
starting off with a 12-bit number but eventually
wind up with a 9-bit result after
scaling. Ideally, I'd like two
optimized routines: 9-bit to Decimal and 12-bit to Decimal.

I suppose that I should have said "binary" instead of "hex".

These are raw (binary) integers - not ASCII.

The problem with John Payson's routine is that it
get fairly long if the final decimal digits are
high-value. That is: a final result of 59999
takes far longer to execute than 11111.

Was just wondering if there is another (faster)
method that works over the range of 0..2047 decimal or 0..511 decimal.

Many thanks!

dwayne
Post by Jan-Erik Söderholm
Hi.
Post by Dwayne Reid
Hex Integer. More than one byte wide, less than two full bytes wide.
What is an "Hex integer"? Is it a binary value? Somewhere between 8 and 16
bits not counting leading zeroes? Or is it a value expressed as hexadecimal
string?
Note that "8F" is not the same as 0x8F. The first has the dual byte binary
value
'0011 1000 0100 0110' (the two characters '8' and 'F' in ASCII encoding).
The second is the single byte binary value '10001111' (but expressed in
hex).
Saying that a PIC processor has something called a "hex value" is not
particular meaningful.
And the conversion is very different if it is from a hex string (in ASCII)
or
if it is really from a binary value.
I *think* that you meant to say a "binary integer".
Post by Dwayne Reid
More than one byte wide...
Does that imply there are no values below 0xFF?
Doesn't matter maybe...
Regards, Jan-Erik.
-----Ursprungligt meddelande-----
Reid
Skickat: den 17 januari 2018 22:13
Ämne: Re: [PIC] Hex to Decimal routines
Post by Dwayne Reid
Do you mean a hex string or an integer? And, Do you mean a string for
output, or bcd?
Hex Integer. More than one byte wide, less than two full bytes wide.
Post by Dwayne Reid
And is this for output to a display, or to a serial port, etc., or
internal storage.
Will be used for driving a 3-digit 7-segment display.
Post by Dwayne Reid
Also, are you wanting a solution which is optimized for code space or
execution speed?
Both <grin>.
Scott Dattalo's 8-bit to Decimal routine (bin2bcd) operates in 28 cycles.
Joun Payson's 16-bit to Decimal routine is longer (don't recall the longest
time).
Post by Dwayne Reid
What language and processor?
Good point. PIC16 family, assembler
I'll include (below) the two routines that I mentioned above.
;Bin2bcd
;convert 8 bit hex # to 3 digit decimal (0..255) ;Scott Dattalo 28 cycles ;
movwf bin
clrf hundreds
swapf bin,W ;add upper & lower nybbles to get 1's digit
addwf bin,W
andlw 0x0F
skpndc ;1's digit binary to bcd conversion
addlw 0x16
skpndc
addlw 0x06
addlw 0x06
skpdc
addlw -0x06
btfsc bin,4 ;bit 4 is special case
addlw 0x16 - 1 + 0x6
skpdc
addlw -0x06
btfsc bin,5 ;2^5 =32 so add 3 to 10's digit if b5==1
addlw 0x30
btfsc bin,6 ;2^6 =64 so add 6 to 10's digit if b6==1
addlw 0x60
btfsc bin,7 ;2^7 =128 so add 2 to 10's digit if b7==1
addlw 0x20
; addlw 0x60 ;convert 10's digit to BCD
; rlf hundreds,F ;propagate C into 100's
; btfss hundreds,0 ;was there in fact a carry?
; addlw -0x60 ;nope - undo conversion
;
; btfsc bin,7 ;input > .200?
; incf hundreds,F ;yep - inc hundreds
;
; movwf bin ;10's & 1's packed BCD result
;use either commented out section above or this below
addlw 0x60 ;convert 10's digit to BCD
skpnc ;overflow?
movlw 0x99
skpc
addlw -0x60 ;
movwf bin ;10's & 1's packed BCD result
return
;;Takes hex number in NumH:NumL Returns decimal in TenK:Thou:Hund:Tens:Ones
;;written by John Payson
TenK EQU LOOPER ;share variables
Thou EQU D2
Hund EQU D1
Tens EQU R2
Ones EQU R1
swapf NumH,w
iorlw b'11110000'
movwf Thou
addwf Thou,f
addlw .226
movwf Hund
addlw .50
movwf Ones
movfw NumH
andlw b'00001111'
addwf Hund,f
addwf Hund,f
addwf Ones,f
addlw .233
movwf Tens
addwf Tens,f
addwf Tens,f
swapf NumL,w
andlw b'00001111'
addwf Tens,f
addwf Ones,f
rlf Tens,f
rlf Ones,f
comf Ones,f
rlf Ones,f
movfw NumL
andlw b'00001111'
addwf Ones,f
rlf Thou,f
movlw 7
movwf TenK
; At this point, the original # ==
TenK*10000+Thou*1000+Hund*100+Tens*10+Ones
; if those entities are regarded as two's compliment binary. To be precise,
; all of them are negative except TenK. Now the number needs to be
normalized, ; but this can all be done with simple byte arithmetic.
movlw .10
addwf Ones,f
decf Tens,f
skpc
goto Lb1
addwf Tens,f
decf Hund,f
skpc
goto Lb2
addwf Hund,f
decf Thou,f
skpc
goto Lb3
addwf Thou,f
decf TenK,f
skpc
goto Lb4
return
Post by Dwayne Reid
Post by Dwayne Reid
Good day to all.
I'm looking for an efficient method of converting a hex number to
decimal. I'm aware of and have used both Scott Dattalo's 8-bit to
Decimal routine as well as John Payson't 16-bit to Decimal routine
but I'm looking for something in-between those two.
I'm starting off with a 12-bit number but eventually wind up with a
9-bit result after scaling. Ideally, I'd like two optimized
routines: 9-bit to Decimal and 12-bit to Decimal.
Any ideas?
Many thanks!
dwayne
--
Trinity Electronics Systems Ltd Edmonton, AB, CANADA
780-489-3199 voice 780-487-6397 fax 888-489-3199 Toll Free
www.trinity-electronics.com
Custom Electronics Design and Manufacturing
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change
your membership options at http://mailman.mit.edu/mailman/listinfo/piclist
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
--
Dwayne Reid <***@planet.eon.net>
Trinity Electronics Systems Ltd Edmonton, AB, CANADA
780-489-3199 voice 780-487-6397 fax 888-489-3199 Toll Free
www.trinity-electronics.com
Custom Electronics Design and Manufacturing
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
John Ferrell
2018-01-18 02:07:33 UTC
Permalink
I believe the fastest way is an array of solutions indexed by the input
values.

If you need faster than that, a hardware diode array will do it in one
machine cycle IF you can load the index that fast.
Post by Dwayne Reid
Was just wondering if there is another (faster)
method that works over the range of 0..2047 decimal or 0..511 decimal.
--
John Ferrell W8CCW
Julian NC 27283
It is better to walk alone,
than with a crowd going the wrong direction.
--Diane Grant
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Ryan O'Connor
2018-01-18 06:44:35 UTC
Permalink
This is confusing because you seem to describe in your most recent reply
that you want a decimal to decimal string (ascii) conversion but the code
you posted does hex to decimal or binary coded decimal conversion.

Ryan
Post by John Ferrell
I believe the fastest way is an array of solutions indexed by the input
values.
If you need faster than that, a hardware diode array will do it in one
machine cycle IF you can load the index that fast.
Post by Dwayne Reid
Was just wondering if there is another (faster)
method that works over the range of 0..2047 decimal or 0..511 decimal.
--
John Ferrell W8CCW
Julian NC 27283
It is better to walk alone,
than with a crowd going the wrong direction.
--Diane Grant
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Dwayne Reid
2018-01-18 16:21:02 UTC
Permalink
I think that semantics is rearing its ugly head.

I wrote:

I thought that my original question was reasonably clear: I
specifically mention that I'm starting off with a 12-bit number but
eventually wind up with a 9-bit result after scaling. Ideally, I'd
like two optimized routines: 9-bit to Decimal and 12-bit to Decimal.

I suppose that I should have said "binary" instead of "hex".

These are raw (binary) integers - not ASCII.

The problem with John Payson's routine is that it get fairly long if
the final decimal digits are high-value. That is: a final result of
59999 takes far longer to execute than 11111.

Was just wondering if there is another (faster) method that works
over the range of 0..2047 decimal or 0..511 decimal.

Many thanks!

dwayne

<end of copy>

John Payson's routine takes a 16-bit binary (2-digit hex) value and
returns the decimal equivalent. I commented that John's routine
takes longer to execute when the final decimal output is 59999 as
compared to smaller decimal output values.

I also specifically mentioned that I'm dealing with binary integers
**NOT** ASCII.

I'm not sure where the confusion is.

dwayne
Post by Ryan O'Connor
This is confusing because you seem to describe in your most recent reply
that you want a decimal to decimal string (ascii) conversion but the code
you posted does hex to decimal or binary coded decimal conversion.
Ryan
Post by Dwayne Reid
Was just wondering if there is another (faster)
method that works over the range of 0..2047 decimal or 0..511 decimal.
--
Dwayne Reid <***@planet.eon.net>
Trinity Electronics Systems Ltd Edmonton, AB, CANADA
780-489-3199 voice 780-487-6397 fax 888-489-3199 Toll Free
www.trinity-electronics.com
Custom Electronics Design and Manufacturing
--
http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive
View/change your membership options at
http://mailman.mit.edu/mailman/listinfo/piclist
Loading...