ascii to decimal conversion
Hubert
Posts: 22
I need a quick and easy way to convert a four digit number from ascii to decimal and I can't seem to find the command.
Is there one?
Is there one?
Comments
The ASCII codes for 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 are $30, $31, $32, $33, $34, $35, $36, $37, $38, $39.
Are you receiving ASCII serial data?· If so, then you would use the DECimal Formatter as part of your SERIN.
Somebody asked a similar question, here's more info, but read up on it in PBASIC HELP just the same -- http://forums.parallax.com/showthread.php?p=569086
Like this:
A VAR Byte
B VAR Byte
C VAR Byte
D VAR Byte
R VAR Word
A = "1"
B = "2"
C = "3"
D = "4" ' for example
R = A - $30
R = R*10 + B - $30
R = R*10 + C - $30
R = R*10 + D - $30
DEBUG "Result is ",DEC R
R will be stored internally and displayed as 1234 decimal.
If the bytes your want converted to decimal are in contigous memory locations you use indexed addressing, like this:
idx VAR Nib
R = 0
FOR idx = 0 TO 3 ' read bytes
R = R*10 + A(idx) - $30
NEXT