Shop OBEX P1 Docs P2 Docs Learn Events
formatting data from Scrath Pad RAM — Parallax Forums

formatting data from Scrath Pad RAM

Jon LoldrupJon Loldrup Posts: 14
edited 2007-05-30 20:23 in BASIC Stamp
Hi

After loading some position-data from my GPS into SPRAM, I am wondering how to convert it to integers?
I can easily put it into the register:

myVariable VAR Byte
GET 0, myVariable

but when I want to convert it to integer:

myOtherVariable VAR Byte
myOtherVariable = DEC myVariable

I am not allowed [noparse]:([/noparse]
How can i EVER do calculations on a digit stored as ASCII-code? Poor me cry.gif

(by the way, if I manage to convert it, and I want to substract two latitudes from each other, I will still have to implement the substraction-algorithm myself, looping through individual bytes containing the individual digits of the position, as a word cannot contain the whole position. Is there no easier way?)


/jon

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
/jon

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2007-05-30 19:53
    You have to do the ASCII digits to integer conversion yourself. A simple loop will do:
    theDigit var byte
    theValue var byte
    position var byte
    convert:
    theValue = 0
    position = 0
    loop:
    get position,theDigit
    position = position + 1
    if (theDigit < "0") or (theDigit > "9") then exit
    theValue = theValue * 10 - (theDigit - "0")
    goto loop
    exit:
    
    


    Note that you can also use DO ... LOOP and other neater constructs.
    This stops at the first non-digit and leaves position at the address of
    the next character after the delimiter. If you need a word value, just
    define theValue as a word instead of a byte.
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-05-30 20:13
    Hello,

    A complete article describing this can be found at the following link. Take care.

    http://www.parallax.com/dl/docs/cols/nv/vol4/col/nv103.pdf

    also

    http://www.parallax.com/dl/docs/cols/nv/vol3/col/nv83.pdf

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2007-05-30 20:17
    Jon,

    Your query relates directly to some PBASIC enhancements I've begun lobbying for: http://forums.parallax.com/showthread.php?p=652397. As BASIC Stamps are used more and more to deal with serial protocols, it would be helpful if the language supported buffer transfers/encoding/decoding in a more I/O-like fashion. Mike's technique will certainly work, but with enhanced language features it wouldn't be necessary.

    -Phil
  • Chris SavageChris Savage Parallax Engineering Posts: 14,406
    edited 2007-05-30 20:23
    Please note also that Mike’s code will only run under 2.0 syntax on the BASIC Stamp Editor since reserved words under 2.5 were used. Take care.

    ▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
    Chris Savage
    Parallax Tech Support
Sign In or Register to comment.