View Full Version : Trying to parse a number into a series of characters
01-09-2008, 01:08 PM
Hi All, first time post.
I'm having a bit of a problem trying to get one thing to turn into another, and I'm at a brain roadblock. Hoping the guru's here have a simple solution.
I have a long variable, with only 4 significant digits (value would be under 9999). What I want to do is to get a series of chr equivalents for these values, so I can write them as a text string character by character into a file. So for the number 1234 I'd need a set of chr's representing the ASCII for "1" "2" "3" "4".
My previous try was to use
numbers.tostr(variable,numbers#SDEC4) := Temp
where Temp was a 5 byte array
and then trying to use each individual index except for Temp to extract characters. That didn't work so well.
Any help anyone can provide would be awesome.
01-09-2008, 01:23 PM
Here's a modified version of the decimal conversion routine used in most of the display type I/O drivers.
It takes a value and the address of a byte array large enough to hold the result terminated by a zero byte.
Maximum length is 12 bytes. You could use STRSIZE() on the address of the byte array to get the length
of the string, then use ordinary byte array access to get the characters.
PUB dec(value,str) | i
'' Print a decimal number
if value < 0
byte[str++] := "-"
i := 1_000_000_000
if value => i
byte[str++] := value / i + "0"
value //= i
elseif result or i == 1
byte[str++] := "0"
i /= 10
byte[str] := 0
01-09-2008, 01:48 PM
Now we're getting somewhere. Still isn't 100%, but it's most likely my implementation
So I declare an array earlier, I used
I used square brackets in the code, but they freak out this editor
then I call that routine
and then I try to read off
again, used the correct bracket in the actual code
to parse out the digits in order, but I'm still not getting things exactly right. Any idea which stupid thing I'm doing wrong? :)
Post Edited (koko76) : 1/9/2008 6:54:31 AM GMT
01-09-2008, 03:28 PM
Highest digit is on the left, so the sequence is textbuff[ 0 ], textbuff [ 1 ]
Second, lowest index to an array is zero, so the first digit is in textbuff [ 0 ]
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
01-09-2008, 04:46 PM
You can also use the Format object (in Object Exchange under Tools).
· fmt: "Format"
· byte buffer
· fmt.sprintf(@buffer,string("%d"),value) 'this does the conversion
· lcd.str(@buffer) 'print value to lcd
· digit0 := byte[@buffer + strsize(@buffer) - 1] 'the lowest digit is at the last place (before null)
Instead of %d (for decimal values) you can also use %x (hexadecimal), %o (octal), %b (binary).
You can even specify left or right justify, with or without leading zeroes.
will right justify any number up to 5 digits with leading spaces if neccessary.
You can add text to the conversion:
· fmt.sprintf(@buffer,string("the value is %d\r\n"),value)
\r\n equals CR,LF
01-09-2008, 11:09 PM
Many thanks guys! Peter, that's exactly what I needed.
01-10-2008, 01:04 AM
Peter won't quite toot his own horn, so I will. Format is here:·http://obex.parallax.com/objects/230/
This library object provides formatted string output and
scan methods based on the standard C sprintf and sscanf
With this object you can convert byte, word and
long types into binary, octal, decimal or hexadecimal
formatted strings. You can specify the minimum and maximum
number of columns to display your values, and these values
can be left or right justified, with or without padded
Methods are included for the following C library
functions: itoa, atoi, sprintf and sscanf.
This implementation defines the functions bprintf and
bscanf that take one, and only one, variable parameter.
The original format string must be split into pieces that
all have one format specifier. This normally is not a
problem since most format strings consists of fixed text
with format specifiers for values.
It's only been up since last month...