Shop OBEX P1 Docs P2 Docs Learn Events
Character To String — Parallax Forums

Character To String

hylee101001hylee101001 Posts: 48
edited 2014-11-17 18:57 in General Discussion
Hello,

I'm trying to get a decimal number from a corresponding number and character comparison.

For example,

I tried to convert '1' to decimal 1
and
I tried to compare x to 'A'
(x := pst.CharIn)

But I do not know how I can do it. I'm confused with using byte array, which I just copied from internet and it doens't work.


This is my code:

CON
_clkmode = xtal1 + pll16x
_xinfreq = 5_000_000


OBJ
pst : "Parallax Serial Terminal"
str: "data"
num : "Numbers"


VAR
long value
byte changed

PUB main | charVar , a
num.Init
pst.Start(115200)
value := 0
repeat
if pst.RxCount > 0
pst.str(String("PRPLR sayz: received"))
charVar := pst.CharIn
pst.str(String("char is: "))
pst.char(charVar)
pst.newline
changed := char 2Dec(charVar)
pst.str(String("number is: "))
pst.Dec(changed)
pst.newline


PUB char 2Dec(charVar) | index
result := 0
repeat index from 0 to strsize(charVar)
result += byte[charVar-3][0]

Comments

  • JonnyMacJonnyMac Posts: 9,105
    edited 2014-11-17 18:57
    If you just have a single character in the range of "0" to "9" you can convert to its decimal value by subtracting "0" -- sometimes I do this in menus that use numbers and letters
    k := ucase(term.rx)
      case k
        "0".."9" : run_process(k-"0")
    
Sign In or Register to comment.