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