At 01:10 PM 1/17/2018, Forrest Christian (List Account) wrote: >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. >And is this for output to a display, or to a serial port, etc., or interna= l >storage. Will be used for driving a 3-digit 7-segment display. >Also, are you wanting a solution which is optimized for code space or >execution speed? Both . 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=20 longest time). >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 digi= t 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 =3D32 so add 3 to 10's digit if b5=3D= =3D1 addlw 0x30 btfsc bin,6 ;2^6 =3D64 so add 6 to 10's digit if b6=3D= =3D1 addlw 0x60 btfsc bin,7 ;2^7 =3D128 so add 2 to 10's digit if b7= =3D=3D1 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:One= s ;;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 # =3D=3D TenK*10000+Thou*1000+Hund*100+Tens*1= 0+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 normali= zed, ; 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 >On Jan 17, 2018 1:18 PM, "Dwayne Reid" wrote: > > > 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 --=20 Dwayne Reid 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 --=20 http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/change your membership options at http://mailman.mit.edu/mailman/listinfo/piclist .