Extracting digits from a number with PICAXE

Hey folks,

Another interesting question - if I want to extract an individual digit from within a longer number (four digits), is there a way to do this? The only formula I could find was [r/(10^(k−1))], where r is the remainder of the number / 10^k, and k is the position of the number you want to extract.

I don't think you can do powers in PICAXE though... Anyone know of a solution?

Many thanks
 

AllyCat

Senior Member
Hi,

Not too difficult if it always has 4 digits.

Code:
w1 = 4321			; Test number
for b0 = 1 to 4			; Position

	lookup b0,(0,1000,100,10,1),w2
	b1 = w1 / w2 // 10

	sertxd(#b1," ")		; Demo result
next
Might need a few more tricks for a position L to R through a variable-length number.

Cheers, Alan.
 

inglewoodpete

Senior Member
There is the 'mathematical' operator 'DIG' - This does not appear to be documented in the html manual but can be found in the PDF Version (P27)

DIG
The DIG (digit) command returns the decimal value of a specified digit (0-4,
right to left) of a 16 bit number. Therefore digit 0 of ‘67890’ is 0 and digit 3 is ‘7’.
To return the ASCII value of the digit simply add string “0” to the digit value e.g.
let b1 = b2 DIG 0 + “0”
See also the BINTOASCII and BCDTOASCII commands.
 
aaaahh.... I saw the X1/X2 mathematical operators in the manual, but I'd never heard of them before - I had no idea how to use them or what they were for. That should sort the problem :)

Using it simply for easy multiplexing of a 4 digit 7-segment display... If i have the 4 digits each stored in an easily accessible nibble on memory, then I can read it very rapidly when it comes to multiplexing. I'll have a completed build up here either this evening or this weekend :)

Cheers once again guys :)
 

inglewoodpete

Senior Member

hippy

Technical Support
Staff member
I am not convinced there needs to be a separate link for mathematical operators when that is covered by the "Variables / let - Perform a mathematical operation" link which is the logical place to describe such operations.

Having the details which are in the PDF manuals also on-line is a valid point and fair request. Details have been provided for a few of the operators and functions but not all. I will see what we can do to correct that oversight.
 

AllyCat

Senior Member
Hi,

Is the on-line version even strictly correct? It says "DIG = return a BCD digit", but isn't it (as requested) the nth character-value from a Decimal number (in Right to Left order, starting at zero)?

Personally, I prefer to use a "universal" instruction, if the OP hasn't specified a specific X2 / M2 chip or family. BINTOASCII is an excellent suggestion (particularly if several digits are required), but it's sometimes worth checking the variable/program size requirements. The core code I posted in #2 requires 17 bytes of codespace whilst BINTOASCII consumes 43 or 47 bytes of program memory (depending on it being M2 or X2), even before subtracting the ASCII "0" (if required).

Cheers, Alan.

PS:
The DIG (digit) command returns the decimal value of a specified digit (0-4,
right to left) of a 16 bit number. Therefore digit 0 of ‘67890’ is 0 and digit 3 is ‘7’.
To return the ASCII value of the digit simply add string “0” to the digit value e.g.
let b1 = b2 DIG 0 + “0”
Hmm. My PE5 and PE6 say "Error: Values greater than 65535 are not supported!" ;)
 
Last edited:
Top