Display unsigned cnt in spin
dbpage
Posts: 217
Do you have an elegant spin method to convert cnt to display unsigned 10 ASCII characters in the range of 0 to 4,294,967,295 without PASM support? It seems easy for someone smarter than me.

Comments
I think you need to test to see if the number if negative and if it's negative split the number into to parts.
I can't think of an easy way to split the number to allow you output the 10 characters but I'm sure this is possible.
True, but it is simple to modify the code to print as an unsigned binary number.
Is it? I've modified dec methods a bunch of times but I can't think of a simple way to output an unsigned 32-bit number.
Spin treats all longs as signed numbers.
temp := CNT ser.dec((temp>>1) / 5) 'show first 9 chars (max.) if temp < 0 temp := POSX-1+temp ser.dec(temp // 10) 'show last charIt's not so simple because *, / and // works also with signed numbers. >> allows an unsigned division by 2.Andy
Edit: There seems to be a Problem with $8000_0000 and $8000_0001 for the lowest digit.
PUB uDec(value) | i, x, s {{ Transmit the ASCII string equivalent of an un-signed decimal value Parameters: value = the numeric value to be transmitted unsigned return: none example usage: serial.uDec(-1) expected outcome of example usage call: Will print the string "4294967295" to a listening terminal. }} s := 0 if value < 0 repeat until value => 0 value -= 1_000_000_000 s++ i := 1_000_000_000 repeat 10 if value => i fds.Tx(value / i + s~ + "0") value //= i result~~ elseif result or i == 1 fds.Tx("0") i /= 10PUB dec_uns(tmp) | last if (tmp => 0) ser.dec(tmp) return last := (tmp // 10) + 6 if (last < 0) last += 10 tmp := (tmp>>1)/5 ser.dec(tmp) ser.dec(last)(Edited for somewhat cleaner code)I think your method always displays a leading zero.
-Phil
The first test for "tmp => 0" ensures that any single digit positive numbers will be printed normally. The rest of the code only deals with unsigned numbers that are much greater than 10 (greater than 2 billion, in fact) so "tmp" will always be non-zero after the divide by 10.
Eric
-Phil
Good work guys. Thanks.
tmp := cnt if tmp<0 or tmp>9 ser.dec(tmp>>1 / 5) ser.dec(tmp>>1 // 5 * 2 + (tmp & 1))Andy
+1 from me.
I appreciate seeing these solutions.
Nice. Elegant.
By controlling the operator precedence levels, following give identical results:
Yet, even though the above snippet results differ, the following give identical results:
Interesting.