Shop OBEX P1 Docs P2 Docs Learn Events
BS2SX array question — Parallax Forums

BS2SX array question

IdtatIdtat Posts: 7
edited 2006-04-18 00:24 in BASIC Stamp
How would I take part of a char string stored in a 10 byte array and save it to a word variable?

ANI VAR Byte(16)
ID VAR Word

ID = STR ANI\7

????????????????

Thanks

Comments

  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-04-17 15:56
    Is it a string of numbers? If yes, you can do a loop to convert to decimal, so long as the value won't exceed 65535.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Jon Williams
    Applications Engineer, Parallax
  • IdtatIdtat Posts: 7
    edited 2006-04-17 22:58
    Its a Char string.....

    Am I outa luck?



    Aaron
  • Jon WilliamsJon Williams Posts: 6,491
    edited 2006-04-18 00:24
    If the string is composed of the ASCII characters "0".."9" you can convert it into a decimal value.· If you run this little demo you'll see that it converts the string to the decimal value 312.

    ' {$STAMP BS2}
    ' {$PBASIC 2.5}
     
    buf             VAR     Byte(5)
    value           VAR     Word
    idx             VAR     Nib
     
    Setup:
      buf(0) = "3"
      buf(1) = "1"
      buf(2) = "2"
      buf(3) = "b"
      buf(4) = "x"
     
    Main:
      DEBUG CLS,
            "String input.... ", STR buf\5, CR
      GOSUB Str_To_Dec
      DEBUG "Decimal value... ", DEC value
      END
     
     
    Str_To_Dec:
      value = 0
      FOR idx = 0 TO 4
        IF (buf(idx) >= "0") AND (buf(idx) <= "9") THEN
          value = value * 10 + (buf(idx) - "0")
        ELSE
          EXIT
        ENDIF
      NEXT
      RETURN
    



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