Shop OBEX P1 Docs P2 Docs Learn Events
Convert "string" to number — Parallax Forums

Convert "string" to number

dk_akjdk_akj Posts: 37
edited 2004-10-02 16:17 in BASIC Stamp
Hi,

I have a received some data via seriel interface.
The data is stored in an array (MSG \8)

The data is always nummeric. ie 00000360
the first 4 chars·is always 0

How do i convert the last 4 chars to a numeric word ie 360·??

Kind regards
Anders

Comments

  • NewzedNewzed Posts: 2,503
    edited 2004-09-30 20:17
    There are two possible ways.· First, since you are receiving the data from outside you could receive it as "dec8 data". then just read "dec4 data".· I think that would do it.· The other way would be to try·

    for x = 3 to 0

    debug dec(x) ? data

    next

    i'm not too sure about that one - you'll have to try it.



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Try the Stamp Tester

    http://hometown.aol.com/newzed/index.html
    ·
  • dk_akjdk_akj Posts: 37
    edited 2004-09-30 20:27
    Hi,

    This gives an error
    SERIN PinRx, Baud,Bad_Data,5000,no_data , [noparse][[/noparse]WAIT("," ),SKIP 1, dec8 limit ]

    This doesnt work:
    SERIN PinRx, Baud,Bad_Data,5000,no_data , [noparse][[/noparse]WAIT("," ),SKIP 1, dec limit ]

    Anders
  • NewzedNewzed Posts: 2,503
    edited 2004-09-30 20:36
    SERIN PinRx, Baud,Bad_Data,5000,no_data , [noparse][[/noparse]WAIT("," ),SKIP 1, dec8 limit ]

    Try this:

    serin pin, baud, 5000, no_data, [noparse][[/noparse]WAIT(","),· dec8 limit]

    I don't think you can use Bad Data and no data in the same command.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Try the Stamp Tester

    http://hometown.aol.com/newzed/index.html
    ·
  • NewzedNewzed Posts: 2,503
    edited 2004-09-30 21:18
    Anders, I was in eror about the Bad Data - that can be used in the same line as your timeout No Data.



    if you are still having trouble, did you check your baud rates?

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Sid Weaver
    Try the Stamp Tester

    http://hometown.aol.com/newzed/index.html
    ·
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-30 21:42
    Anders,

    Assuming that you're successfully receiving the string (of eight characters), here's how you can extract the last four characters as a numeric value:

    Get_Value:
    · value = 0
    · FOR idx = 4 TO 7
    ··· value = value * 10
    ··· value = value + (msg(idx) - "0")
    · NEXT
    · RETURN

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


    Post Edited (Jon Williams) : 9/30/2004 10:12:40 PM GMT
  • NewzedNewzed Posts: 2,503
    edited 2004-09-30 21:52
    Jon, wouldn't that read the four lefthand digits instead of the righthand four digits?

    Sid
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2004-09-30 22:13
    Nope -- give it a try. I did.· The cool thing about the BASIC Stamp is that it's faster to test theories than to post questions about them.

    msg············ VAR···· Byte(8)
    idx············ VAR···· Nib
    value·········· VAR···· Word

    Reset:
    · DEBUG CLS, "Enter a value (8 digits): "

    Main:
    · DEBUGIN STR msg\8
    · value = 0
    · FOR idx = 4 TO 7
    ··· value = value * 10
    ··· value = value + (msg(idx) - "0")
    · NEXT
    · DEBUG CRSRXY, 0, 2, DEC value
    · END



    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
    Dallas Office
  • dk_akjdk_akj Posts: 37
    edited 2004-10-02 16:17
    Thanks, It works great.

    //akj
Sign In or Register to comment.