Int to String

John BoardJohn Board Posts: 371
edited January 2012 in Propeller 1 Vote Up0Vote Down
Hi,

As a quick question, how would I convert an int to a string in SPIN?

For example, if I wanted to send an int to a parallax serial LCD screen, how would I convert it so that the screen understands what I am wanting to display?

Thanks,

John

Comments

  • 5 Comments sorted by Date Added Votes
  • Martin_HMartin_H Posts: 3,984
    edited January 2012 Vote Up0Vote Down
    I use a serial LCD and use the serial terminal's Dec method to convert. But if you wanted to write to a buffer you could reuse that code inside your object.
  • John BoardJohn Board Posts: 371
    edited January 2012 Vote Up0Vote Down
    Thanks, I hadn't even thought about FullDuplexSerial's dec method...
  • JonnyMacJonnyMac Posts: 5,739
    edited January 2012 Vote Up0Vote Down
    This is a method that I add to a variant of FDS that I use in my projects -- it's especially useful for LCDs that might be used for instrumentation type displays.
    pub rjdec(val, width, pchar) | tmpval, pad
    
    '' Print right-justified decimal value
    '' -- val is value to print
    '' -- width is width of (space padded) field for value
    
    '  Original code by Dave Hein
    '  Added (with modifications) to FDS by Jon McPhalen
    
      if (val => 0)                                                                 ' if positive
        tmpval := val                                                               '  copy value
        pad := width - 1                                                            '  make room for 1 digit
      else
        if (val == negx)                                                            '  if max negative
          tmpval := posx                                                            '    use max positive for width
        else                                                                        '  else
          tmpval := -val                                                            '    make positive
        pad := width - 2                                                            '  make room for sign and 1 digit
       
      repeat while (tmpval => 10)                                                   ' adjust pad for value width > 1
        pad--
        tmpval /= 10
         
      repeat pad                                                                    ' print pad
        tx(pchar)
    
      dec(val)                                                                      ' print value
    

    Note that rjdec calls the dec method.
    Jon McPhalen
      *It's "Jon" or "JonnyMac" -- please don't call me "Jonny"
  • max72max72 Posts: 1,124
    edited January 2012 Vote Up0Vote Down
    check also the basic number formatting objecs:
    numbers (included in the propeller tool)
    simple numbers http://obex.parallax.com/objects/182/

    I really like simple numbers because it is easier to use, and has some formatting options not available in numbers (some fixed format options, for instance).
    Massimo
  • I case anyone else is interested it seems this is now at.
    obex.parallax.com/object/536
Sign In or Register to comment.