PDA

View Full Version : Trying to parse a number into a series of characters

koko76
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[0] to extract characters. That didn't work so well.

Any help anyone can provide would be awesome.

Mike Green
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
-value
byte[str++] := "-"

i := 1_000_000_000

repeat 10
if value => i
byte[str++] := value / i + "0"
value //= i
result~~
elseif result or i == 1
byte[str++] := "0"
i /= 10
byte[str] := 0

koko76
01-09-2008, 01:48 PM
Ok,
Now we're getting somewhere. Still isn't 100%, but it's most likely my implementation

So I declare an array earlier, I used
textbuf(5)
I used square brackets in the code, but they freak out this editor

then I call that routine
dec(variable,@textbuf)

and then I try to read off

textbuf(4)
textbuf(3)
textbuf(2)
textbuf(1)

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

Nick Mueller
01-09-2008, 03:28 PM
> textbuf(4)
> textbuf(3)
> textbuf(2)
> textbuf(1)

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 ]

Nick

▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!

The DIY Digital-Readout for mills, lathes etc.:

Peter Verkaik
01-09-2008, 04:46 PM
You can also use the Format object (in Object Exchange under Tools).

OBJ
· fmt: "Format"

VAR
· byte buffer[10]

PUB
· someFunction
· 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.
· fmt.sprintf(@buffer,string("%5.5d"),value)
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

regards peter

koko76
01-09-2008, 11:09 PM
Many thanks guys! Peter, that's exactly what I needed.

Fred Hawkins
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
functions.

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
zeros.

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...