>-----Original Message----- >From: Chris Loiacono [mailto:chris@MAIL2ASI.COM] >Sent: Monday, June 09, 2003 12:39 PM >To: PICLIST@MITVMA.MIT.EDU >Subject: char to int > > >Earlier this month there was a brief discussion on converting integers = to >characters. > >Is there a simple gem for converting characters to integers? > Just off the top of my head, (WARNING, UNTESTED PSEUDO_CODE FOLLOWS) Not optimized, just fairly easy to read. Could use less space and registers. Written for 18F instruction set. Will read an arbitrary number of digits until a non-numeric digit is read. That digit is "lost". Reads from "getnext" For 0-255, with or without leading zeros, decimal to binary whole numbers only. A decimal point will terminate input. ten RES 01 result RES 01 scratch RES 01 ASC2BIN movlw .10 movwf ten clrf result loop call getnext ;get next digit into W ;convert from ASCII to value sublw '0' ;subtract ASCII Zero ;test for non-numeric data bn outtahere ;character less than '0', we are done cpfslt ten ;skip if result less than 10 bra outtahere ;character greater than '9', we are done =09 ;calculate new result movwf scratch ;intermediate storage of new digit movf result,w ;get current result into W mullw .10 ;multiply current result by ten (into PRODL) movf PRODL,w addwf scratch ;add newest digit movwf result bra loop ;go get next character outtahere ;conversion complete movf result,w return =09 I hope I got it right. If not as working code then at least an example of method. I am quite sure there are many here with shorter/faster/more efficient/better featured ways. Should be fairly easy to rewrite for 16 bit results. Good Luck, (Hoping to avoid flames for less than perfect code) Lyle -- http://www.piclist.com#nomail Going offline? Don't AutoReply us! email listserv@mitvma.mit.edu with SET PICList DIGEST in the body