Convert ASCII Decimal

TGN

New Member
I have a problem to solve this
I have ASCII value in RAM like this "1234" or B0="1" ,B1="2", B2="3", B3="4"
Now i want the word value like this w7=1234
Can u help

tanks
 

vttom

Senior Member
w7 = b0 - 48 * 1000
w7 = b1 - 48 * 100 + w7
w7 = b2 - 48 + 10 + w7
w7 = b3 - 48 + w7

or, to steal an idea that hippy posted not too long ago in another thread, you can put it all in 1 line (remembering that the PICAXE order of operations is strictly left-to-right)

w7 = b1 - 48 * 10 + b2 - 48 * 10 + b3 - 48 * 10 + b4 - 48
 

inglewoodpete

Senior Member
Alternatively, just using RAM, bPtr and a word register:
Code:
Poke 100, "1", "2", "3", "4"  'Initialise RAM
'
w1 = 0                        'Just to be safe
For bPtr = 100 To 103
   w1 = w1 * 10 + @bPtr - 48
Next bPtr
'	
SerTxd("Result is ", #w1, CR, LF)
If we exclude the Poke and the SerTxd, 23 bytes of code. The code above can be run in the simulator.
 
Top