Shop OBEX P1 Docs P2 Docs Learn Events
Int to String — Parallax Forums

Int to String

John BoardJohn Board Posts: 371
edited 2012-01-31 02:54 in Propeller 1
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

  • Martin_HMartin_H Posts: 4,051
    edited 2012-01-30 17:56
    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 2012-01-30 18:13
    Thanks, I hadn't even thought about FullDuplexSerial's dec method...
  • JonnyMacJonnyMac Posts: 9,104
    edited 2012-01-30 19:34
    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.
  • max72max72 Posts: 1,155
    edited 2012-01-31 02:54
    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.