Shop OBEX P1 Docs P2 Docs Learn Events
Trying to parse a number into a series of characters — Parallax Forums

Trying to parse a number into a series of characters

koko76koko76 Posts: 4
edited 2008-01-09 18:04 in Propeller 1
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.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2008-01-09 06:23
    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[noparse][[/noparse]str++] := "-"
    
      i := 1_000_000_000
    
      repeat 10
        if value => i
          byte[noparse][[/noparse]str++] := value / i + "0"
          value //= i
          result~~
        elseif result or i == 1
          byte[noparse][[/noparse]str++] := "0"
        i /= 10
      byte[noparse][[/noparse]str] := 0
    
    
  • koko76koko76 Posts: 4
    edited 2008-01-09 06:48
    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? [noparse]:)[/noparse]
    
    
    Post Edited (koko76) : 1/9/2008 6:54:31 AM GMT
  • Nick MuellerNick Mueller Posts: 815
    edited 2008-01-09 08:28
    > textbuf(4)
    > 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
  • Peter VerkaikPeter Verkaik Posts: 3,956
    edited 2008-01-09 09:46
    You can also use the Format object (in Object Exchange under Tools).

    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
  • koko76koko76 Posts: 4
    edited 2008-01-09 16:09
    Many thanks guys! Peter, that's exactly what I needed.
  • Fred HawkinsFred Hawkins Posts: 997
    edited 2008-01-09 18:04
    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.
     
    [b]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.[/b] 
     
    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...
Sign In or Register to comment.