Trying to parse a number into a series of characters
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[noparse][[/noparse]0] to extract characters. That didn't work so well.
Any help anyone can provide would be awesome.
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[noparse][[/noparse]0] to extract characters. That didn't work so well.
Any help anyone can provide would be awesome.
Comments
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.
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? [noparse]:)[/noparse]
Post Edited (koko76) : 1/9/2008 6:54:31 AM GMT
> textbuf(3)
> textbuf(2)
> textbuf(1)
Highest digit is on the left, so the sequence is textbuff[noparse][[/noparse] 0 ], textbuff [noparse][[/noparse] 1 ]
Second, lowest index to an array is zero, so the first digit is in textbuff [noparse][[/noparse] 0 ]
Nick
▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔▔
Never use force, just go for a bigger hammer!
The DIY Digital-Readout for mills, lathes etc.:
YADRO
OBJ
· fmt: "Format"
VAR
· byte buffer[noparse][[/noparse]10]
PUB
· someFunction
· fmt.sprintf(@buffer,string("%d"),value) 'this does the conversion
· lcd.str(@buffer) 'print value to lcd
· digit0 := byte[noparse][[/noparse]@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
It's only been up since last month...