Hi there, Jan-Erik. I thought that my original question was=20 reasonably clear: I specifically mention that I'm=20 starting off with a 12-bit number but eventually=20 wind up with a 9-bit result after=20 scaling. Ideally, I'd like two=20 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=20 get fairly long if the final decimal digits are=20 high-value. That is: a final result of 59999=20 takes far longer to execute than 11111. Was just wondering if there is another (faster)=20 method that works over the range of 0..2047 decimal or 0..511 decimal. Many thanks! dwayne At 03:15 PM 1/17/2018, Jan-Erik S=F6derholm wrote: >Hi. > > > 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 hexadecima= l >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". > > > More than one byte wide... > >Does that imply there are no values below 0xFF? >Doesn't matter maybe... > >Regards, Jan-Erik. > > > > >-----Ursprungligt meddelande----- >Fr=E5n: piclist-bounces@mit.edu [mailto:piclist-bounces@mit.edu] F=F6r Dwa= yne >Reid >Skickat: den 17 januari 2018 22:13 >Till: Microcontroller discussion list - Public. >=C4mne: Re: [PIC] Hex to Decimal routines > >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 > >internal 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 longes= t >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 di= git > 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:On= es >;;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*10+Ones >; if those entities are regarded as two's compliment binary. To be precis= e, >; 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 > > > > > > >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 > > >-- >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 > >-- >http://www.piclist.com/techref/piclist PIC/SX FAQ & list archive View/chan= ge >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 --=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 .