Conver Scratch pad-string to numeric value on Bs2p
Archiver
Posts: 46,084
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.
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
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:
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:
-- Jon Williams
-- Parallax
Original Message
From: