Int to String

John BoardJohn Board Posts: 371
edited 2012-01-31 - 02:54:39 in Propeller 1

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?




  • Martin_HMartin_H Posts: 4,050
    edited 2012-01-30 - 17:56:06
    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:22
    Thanks, I hadn't even thought about FullDuplexSerial's dec method...
  • JonnyMacJonnyMac Posts: 6,661
    edited 2012-01-30 - 19:34:42
    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
        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
        tmpval /= 10
      repeat pad                                                                    ' print pad
      dec(val)                                                                      ' print value

    Note that rjdec calls the dec method.
  • max72max72 Posts: 1,136
    edited 2012-01-31 - 02:54:39
    check also the basic number formatting objecs:
    numbers (included in the propeller tool)
    simple numbers

    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).
  • I case anyone else is interested it seems this is now at.
Sign In or Register to comment.