Shop OBEX P1 Docs P2 Docs Learn Events
Integer (DEC) to String Conversion. — Parallax Forums

Integer (DEC) to String Conversion.

BorisBoris Posts: 81
edited 2004-10-20 16:55 in BASIC Stamp
I have a BYTE-size variable with an integer 0 -> 255 written in it (variable Temp, for example)
what i need to do is use that number in a SERIN (WAIT)·command, in a loop.
The wait qualifier will have a 3 set charachters (ABC)·and variable Temp appeneded to it. If i leave Temp as an integer basic stamp will convert it to an ASCII charachter. But i need it to wait for ABC25, ABC26, ABC27, etc...
or 3 digit ABC134, ABC135, 136,137,138...
There does not appear to be a String command to automatically convert DEC to String.
I came up with this pretty complicated procedure:
1) Find out number of digits (0 to 9-1, 10 to 99-2, 100 to 255-3)
2) Take each digit, one at a time,·(depending on number of digits), and add 48 to it, to convert to ASCII character equal to that number, then write the ASCII character into Temp2(0 to 3).
3) Now here is the problem, if i just leave Temp2 as is,·consisting of·3 bytes, and do WAIT("ABC",Temp), then if the original integer was less than 100 (lets say it was 2 digits), the command will actually wait for ABC0##, i need it to WAIT for ABC##

The integer to decimal conversion seems like a pretty simple and common process.
Any ideas on how to complete step 3 of my logic? or
Is there an already written code that does this?

Thank you,

Boris.

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-10-19 22:12
    Look in the Help file or manual under SERIN and checkout the section on WAITSTR. You'll have to construct the string yourself, but PBASIC will wait on it for you.

    It's easy to create a subroutine to build the string to wait for ... like this:

    Make_Wait_Str:
      wStr(0) = "A"
      wStr(1) = "B"
      wSTr(2) = "C"
      IF (idx < 100) THEN
        wStr(3) = (idx DIG 1) + "0"
        wStr(4) = (idx DIG 0) + "0"
        wStr(5) = 0
        wLen = 5
      ELSE
        wStr(3) = (idx DIG 2) + "0"
        wStr(4) = (idx DIG 1) + "0"
        wStr(5) = (idx DIG 0) + "0"
        wLen = 6
      ENDIF
      RETURN
    

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office


    Post Edited (Jon Williams) : 10/19/2004 10:20:52 PM GMT
  • BorisBoris Posts: 81
    edited 2004-10-20 15:36
    And if i have numbers from 0 to 255 the code will look like this right?
    Make_Wait_Str:
      wStr(0) = "A"
      wStr(1) = "B"
      wSTr(2) = "C"
      IF (idx < 10) THEN
        wstr(3) = (idx DIG 0) + "0"
        wstr(4) = 0
        wstr(5) = 0
        wLen = 4
      ELSEIF (idx < 100) THEN
        wStr(3) = (idx DIG 1) + "0"
        wStr(4) = (idx DIG 0) + "0"
        wStr(5) = 0
        wLen = 5
      ELSE
        wStr(3) = (idx DIG 2) + "0"
        wStr(4) = (idx DIG 1) + "0"
        wStr(5) = (idx DIG 0) + "0"
        wLen = 6
      ENDIF
      RETURN
    

    what is variable wLen used for?
    And the SERIN command will look like this?
    SERING Rpin, baudmode,[noparse][[/noparse]WAITSTR wstr]
    so if idx=>100 wstr will be "###", 2 digits it will be "##" or "## "? 1 digit "#" or "#· "
    Thanks for your input.
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-10-20 16:55
    Yes, you've got it. Sorry I forgot to clarify wLen -- use it like this:

    SERIN pin, baud, [noparse][[/noparse]WAITSTR wStr\wLen]

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
Sign In or Register to comment.