Shop OBEX P1 Docs P2 Docs Learn Events
ascii to decimal conversion — Parallax Forums

ascii to decimal conversion

HubertHubert Posts: 22
edited 2006-02-02 04:46 in BASIC Stamp
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?

Comments

  • PJAllenPJAllen Banned Posts: 5,065
    edited 2006-02-02 02:23
    The ASCII codes themselves are, by design, the numbers.

    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
  • BullwinkleBullwinkle Posts: 101
    edited 2006-02-02 04:46
    Just subject "0" (letter zero or $30 (hex)) from each byte, turning them into their "real" decimal equivalent. Moving from left to right, multiply result by 10 and add the next digit subtracting "0" each time.

    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
Sign In or Register to comment.