ASCII characters to decimal number

russbow

Senior Member
I need to convert an ascii string received by serin to a decimal number.
This code works in the simulator.

Code:
#picaxe18m2

'Set up artificial string

eeprom 0,("1234")

do

read 0,b0,b1,b2,b3	'Read back ASCII values


 sertxd (b0,b1,b2,b3,cr,lf)
 
 pause 1000
 
 'Convert ASCii to decimal
 
 for bptr =0 to 3
 
 @bptr=@bptr-48
 
 next bptr
 
 sertxd ("Decimal ",#b0," ",#b1," ",#b2," ",#b3,cr,lf)
 
 pause 2000
 
 'Scale place values
 
 w2=b0*1000
 w3=b1*100:w2=w2+w3
 w3=b2*10:w2=w2+w3
 w3=b3:w2=w2+w3
 
 sertxd ("W2 = ",#w2,cr,lf)

 
 
 pause 2000
 
 loop
Although I get expected results, is there a better way to do it. Can you see any pitfalls

Thanks,

R.
 

westaust55

Moderator
you indicate you have EEPOM data as an artificial string.

Where will the final data be coming from?

DO you need SERIN pin, baud, #w3 ; to receive an ASCII string of up to 5 digits and automatically strips the ASCII encoding to put the value into a word variable
or SERRXD #w3 ; possible if coming from the PC
 

hippy

Ex-Staff (retired)
w2=b0*1000
w3=b1*100:w2=w2+w3
w3=b2*10:w2=w2+w3
w3=b3:w2=w2+w3

Can become a more simpler ...

w2 = b0 * 10 + b1 * 10 + b2 * 10 + b3

PICAXE maths is strictly left to right, no operator precedence, hence why all are * 10.
 

russbow

Senior Member
@Nick. Thanks but I need to combine 4 characters to a number.

@ Westy
DO you need SERIN pin, baud, #w3 ; to receive an ASCII string of up to 5 digits and automatically strips the ASCII encoding to put the value into a word variable
or SERRXD #w3 ; possible if coming from the PC
Sort of. Ascii string sent via RF link to base 18m2. Working fine as is, and all displays ok. Have a thought might want to log min / max values received. Hence requirement to convert to decimal.

@Hippy
w2 = b0 * 10 + b1 * 10 + b2 * 10 + b3
As ever, spot on. I tried
Code:
 w2=b0*1000+b1*100+b2*10+b3
and of course got the wrong answer. Need to get a bit of paper and do the 10*10*10 bit.

Thanks to you all.
 

Armp

Senior Member
If you don't need the individual digits

Code:
#picaxe18m2

'Set up artificial string

eeprom 0,("1234")

read 0,b0,b1,b2,b3	'Read back ASCII values
 
bptr =0 
 
w2 = @bptrinc * 10 +@bptrinc * 10 +@bptrinc * 10 +@bptrinc - 53328
 
Last edited:

russbow

Senior Member
Why is it so obvious when shown the right way, even though it looks 'wrong'

Code:
w2 = b0 * 10 + b1 * 10 + b2 * 10 + b3
So the progression is 1 > 10 > 12 > 120 > 123 > 1230 > 1234

@Armp. Havn't tried your code yet. Why the - 53328 at the end? Something to do with stripping back the ascii values ??
 

Armp

Senior Member
Yup - its 48 + 480 +4800 + 48000....
The code snippet above will run in PE - try it.

I store directly from serin into RAM, then just point bptr to the beginning of the buffer - no variables required.

Code:
Bptr = $40
serin inpin, baud, @bptrinc, @bptrinc, @bptrinc, @bptrinc
Bptr = $40
w2 = @bptrinc * 10 +@bptrinc * 10 +@bptrinc * 10 +@bptrinc - 53328
 

russbow

Senior Member
Like it !! Although you say ' ... no variables required ... ' they are of course used and retrievable as b0 etc.

I guess this is only good for a max of four ascii characters.

Yup - its 48 + 480 +4800 + 48000....
Thanks for the tips

R.
 

Armp

Senior Member
Like it !! Although you say ' ... no variables required ... ' they are of course used and retrievable as b0 etc.
Nope! I set Bptr=$40 so the characters are stored in the RAM area, not the 'variable' area.
Leave it at 0 if you WANT to use b0...

I guess this is only good for a max of four ascii characters.
How many digits did you want?
 

westaust55

Moderator
@ Westy

Sort of. Ascii string sent via RF link to base 18m2. Working fine as is, and all displays ok. Have a thought might want to log min / max values received. Hence requirement to convert to decimal.

Thanks to you all.
What do you mean by "sort of"?
If you have a number up to 5 digits coming in ASCII encoded and followed by say "cr" or other non "0" to "9" then
SERIN with #w2
Accepts the string and auto strips ASCII leaving you with the basic decimal number.
So no need to save individual ASCII characters and recombine.
Or am I missing something?
 

russbow

Senior Member
@Westy. Sorry, you are not missing anything, my bad choice of wording and my usual unclear explanation.
Yes, I receive four ascii characters via an RF serin, and I want to combine them to a decimal.

Thus - "1", "2", "3", "4" to equal 1234. The code was just a routine to try my ideas. As usual came a cropper on the integer maths but got it working. Now fully understand the ways suggested.

The string I require is the 4th to 7th character, so I serin b0,b0,b0,b0,b1........

Hope that makes it clearer
 

westaust55

Moderator
Ah yes, thanks, that clears up the situation.
You are parsing a data string and extracting 4 characters from that string.
As a result the #w2 for SERIN would/may not work even if you skip the first 4 characters unless you know the next character is always a non "0" to "9" to terminate the SERIN accepting dfata for word variable w2.
 

Armp

Senior Member
The string I require is the 4th to 7th character, so I serin b0,b0,b0,b0,b1........
I try not to use variables for temp storage....

Code:
Bptr = $40
serin inpin, baud, @bptr, @bptr, @bptr, @bptrinc, @bptrinc, @bptrinc, @bptrinc
Bptr = $40
w2 = @bptrinc * 10 + @bptrinc * 10 + @bptrinc * 10 + @bptrinc - 53328
 
Top