Shop OBEX P1 Docs P2 Docs Learn Events
Conver Scratch pad-string to numeric value on Bs2p — Parallax Forums

Conver Scratch pad-string to numeric value on Bs2p

ArchiverArchiver Posts: 46,084
edited 2003-11-04 01:25 in General Discussion
I use a Pak6a to get read a keyboard.
The key is stored by:

Store:
Put Adr,KeyIn
Adr=Adr+1 ' So next key is stored on the next adress.
Return

When I need to convert from string to numeric value:

Convert:
Number=0
For I=0 to Adr-1 '-1 cause adr is +1 every time key is stored
GET I,KeyIn 'Get digit from field
Number = Number + (KeyIn - "0") 'convert add into value
Number = Number * 10 ' shift result digits left
Next
Debug Dec number

But the convertion don't work.
Is there a better way to convert string to numbers?

Stein.

Comments

  • ArchiverArchiver Posts: 46,084
    edited 2003-11-03 23:12
    Be careful using STORE as a label since this is a PBASIC keyword for the BS2p
    and BS2pe.

    I wonder if your convesion issue has to do with the order your keys are entered.
    Let's say, for example you enter the number 325. I would assume, based on your
    code that it would appear in the Scratcpad as follows:

    0 : 3
    1 : 2
    2 : 5

    If this is the case, you need to to update your convesion like this:

    Convert_Num:
    number = 0
    FOR idx = 0 TO (addr - 1)
    number = number * 10
    GET idx, keyIn
    number = number + (keyIn - "0")
    NEXT

    The problem is that you're shifting "late" which will cause the 1's digit to be
    multiplied by 10 unnecessarily.

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



    Original Message
    From:
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-03 23:31
    Jon, you are a genius.
    It's working exactly as you wrote.

    eeee....I did not use "store" in my code.
    Just made one up when I made the massage.

    By the way....will the Scratch pad Ram be worn out like the Eeprom?

    Stein.

    --- In basicstamps@yahoogroups.com, "Jon Williams" <jwilliams@p...>
    wrote:
    > Be careful using STORE as a label since this is a PBASIC keyword
    for the BS2p and BS2pe.
    >
    > I wonder if your convesion issue has to do with the order your
    keys are entered. Let's say, for example you enter the number 325.
    I would assume, based on your code that it would appear in the
    Scratcpad as follows:
    >
    > 0 : 3
    > 1 : 2
    > 2 : 5
    >
    > If this is the case, you need to to update your convesion like
    this:
    >
    > Convert_Num:
    > number = 0
    > FOR idx = 0 TO (addr - 1)
    > number = number * 10
    > GET idx, keyIn
    > number = number + (keyIn - "0")
    > NEXT
    >
    > The problem is that you're shifting "late" which will cause the
    1's digit to be multiplied by 10 unnecessarily.
    >
    > -- Jon Williams
    > -- Applications Engineer, Parallax
    > -- Dallas Office
    >
    >
    >
    >
    Original Message
    > From:
  • ArchiverArchiver Posts: 46,084
    edited 2003-11-04 01:25
    The Scratchpad is RAM, not EEPROM, so you won't wear it out.

    -- Jon Williams
    -- Parallax


    Original Message
    From:
Sign In or Register to comment.