ascii to decimal conversion

besam

New Member
what is the best way of converting a number like 350 being sent in ascii format (recieved via serial port to b0, b1 and b2) to a decimal? ie how do i recreate the number 350 in true decimal in the picaxe?

 
 

hippy

Technical Support
Staff member
It would probably be better if you kept all questions related to an ongoing topic in the thread you initially started rather than asking related questions all over the place.

The easiest way to do what you want is ...

- SERIN pin,baud,b2,b1,b0
- w0 = b2 * 10 + b1 * 10 + b0 - 5328
 
Last edited:

RickAlty

Senior Member
Hippy:
"The easiest way to do what you want is ...

- SERIN pin ,baud ,b2,b1,b0"

Why wouldn't it be eaier to just go

- SERIN pin, baud #b2,#b1,#b0 ?
 

hippy

Technical Support
Staff member
The problem there is that all numeric digits will get accumulated into b2 rather than three single characters going into each byte variable.

Using #var also requires sending a terminating character so the SERIN knows when the number has been entered or it would just wait after three digits incase there were any more to come.
 

wilf_nv

Senior Member
A string of BCD numbers, for example 999, sent from a PC serial port to a PICAXE serial port, formatted "9 9 9" is compatible with receiving them with serin, pin, baud, #b3,#b1,#b0
 

MPep

Senior Member
Hi People,

I have also had a requirement for this.
Hippy's code is almost there, apart from the fact that ASCII characters, in Hex, are $30 - $39.

The final code needs to look like this:
w4=b0-$30*10+b1-$30*10+b2-$30

Initially, I thought that b0 needed to be multiplied by 100, but not so. Can't figure that out yet.

If your numbers coming back (in ASCII) are below 255, then instaed of using w4, you can use b4.
DEBUG is your friend here, for verification purposes.

Look at Right Hand Side for ASCII input, and look at first column to right of b4 to view converted number.

Regards,
MP

 
 

hippy

Technical Support
Staff member
Almost there ? I'd say it was perfect :)

I should have perhaps explained the theory of how the code works, so here goes ...

((b2-$30)*100) + ((b1-$30)*10) + (b0-$30)

(b2*100)-($30*100) + (b1*10)-($30*10) + (b0)-($30)

(b2*100) - ($30*100) + (b1*10) - ($30*10) + (b0) - ($30)

(b2*100) + (b1*10) + (b0) - ($30*100) - ($30*10) - ($30)

(b2*100) + (b1*10) + (b0) - ( ($30*100) + ($30*10) + ($30) )

(b2*100) + (b1*10) + (b0) - ( (48*100) + (48*10) + (48) )

(b2*100) + (b1*10) + (b0) - ( 4800 + 480 + 48 )

(b2*100) + (b1*10) + (b0) - ( 5328 )

( (b2*10) * 10 ) + (b1*10) + (b0) - ( 5328 )

( ( (b2*10) + b1 ) * 10 ) + (b0) - ( 5328 )

b2 * 10 + b1 * 10 + b0 - 5328

The last step is the hard one to grasp and always looks wrong when I look at, why it is "b2 * 10"; and not "b2 * 100". That is because the PICAXE does all maths strictly left to right so the "b2 * 10" is multiplied by 10 again after b1 has been added to it.

ASCII codes are not necessarily in hexadecimal, but can be said to be in any base one cares to imagine them to be. Whether $30, 48 or %110000 is used is really a matter of preference, as they all represent the same value. The most readable code would perhaps be ...

- w4 = b2-"0" * 10 + b1-"0" * 10 + b0-""0"

The order I have my variables differs to your solution, but that is determined by the order in which the digits are read into the respective bytes by the SERIN.

Although there's nothing wrong with the solution you suggest, it's not as optimised as mine, but the code saving is only just over a byte, so there's not a lot in it.

Edited by - hippy on 24/08/2006 06:11:38
 
Last edited:

MPep

Senior Member
Hi there Hippy,

Initially your code did not make a lot of sense to me. Thanks for your explanation.

I now also understand the b2*10 vs b2*100 bit.

Initially did not see the -5328 bit. Sorry.

I had read that maths is strictly L-R. This can be annoying. would prefer the use of brackets as per correct mathematics.

Oh well, 2 solutions to 1 problem. And I am guessing that these are only just a few of what others may have worked out.

MP

Edited by - MPep on 27/08/2006 22:08:18
 

hippy

Technical Support
Staff member
The PICAXE cannot use brackets or operator precedence beacuse it doesn't have a stack or anywhere to store intermediate calculations, so it has to be this left to right method. At least it's not Reverse Polish Noation (RPN) ...

LET w0 = b0 + 10 * b1 + 10 * b0 + 5328 -
 

RickAlty

Senior Member
Isn't the acronym for Reverse Polish Notation "NPR" ?

(I still have an HP calculator somewhere in a drawer on my desk that uses it - though I doubt if I can still remember how to use it !)
 
Top