Shop OBEX P1 Docs P2 Docs Learn Events
inserting number into string — Parallax Forums

inserting number into string

pgbpsupgbpsu Posts: 460
edited 2010-10-19 12:58 in Propeller 1
How can I insert a number into a string?

I have a device that I can put to sleep using a serial port. The following command works just fine:
  uarts.str(port,string("$PRTHS,COMA,86400000",CR,LF))

But I want to sleep the device for differing amounts of time. I'd like to be able to replace the 86400000 with a number of my choice. Here's what I've come up with, which doesn't seem to work by the way:
  len := strsize(Num.ToStr(mSeconds, Num#DEC)) 
  uarts.str(debug,string(13,"len: " ))
  uarts.dec(debug,len)
  uarts.putc(debug,CR)
  bytefill(@sleepString,0,25)                    ' init sleep string
  bytefill(@msString,0,10)                       ' init ms string
  bytemove(@sleepString,@comaString,12)          ' copy first part ($PRTHS,COMA,) into string
  bytemove(@msString,Num.ToStr(mSeconds, Num#DEC),len)   ' convert dec into string and store
  bytemove(@sleepString[12],@msString[1],len-1)  ' copy dec-string into final destination but leave off space preceeding number
  sleepString[11+len]    := CR  '                ' add CR
  sleepString[11+len+1]  := LF  '                ' add LF
  sleepString[11+len+2]  := 0   '                ' make sure it's zero terminated

  uarts.str(debug,@sleepString)
  uarts.putc(debug, CR)

  repeat len from 0 to 24
    uarts.hex(debug,sleepString[len],2)
    uarts.putc(debug,SPACE)



Here's what I get:
len: 9
$PRTHS,COMA,80044000

24 50 52 54 48 53 2C 43 4F 4D 41 2C 38 30 30 34 34 30 30 30 0D 0A 00 00 00

The hex values actually look fine so I'm not sure why it doesn't work. Maybe the question I should be asking is how can I spit out the hex characters for the string (at top) that does work?

And is there a simpler way to do this?

Thanks,
Peter

Comments

  • Heater.Heater. Posts: 21,230
    edited 2010-10-18 21:42
    pgbpsu,

    Let's get this straight. You want to spit out the string "$PRTHS,COMA,86400000" followed by a carriage return and new line.

    So why not just:
     uarts.str(debug, "$PRTHS,COMA,")
     uarts.dec(debug, 86400000)               'Or whatever variable here
     uarts.putc(debug, CR)
     uarts.putc(debug, LF)
    

    That assumes that whatever object "uarts" is has a "dec" method to output a decimal value as an ASCII string of digits. If it has not you will have to do that conversion first with "Num.ToStr" and just output the resulting string at that point.

    Your example shows the correct looking string being output to the debug port. Does your actual device use the same baud rate?
  • pgbpsupgbpsu Posts: 460
    edited 2010-10-19 06:34
    Heater-

    That solved it!

    There is no excuse for how I was trying to do this. I don't know how I made something so simple so complicated. Clearly that's why it didn't work.

    Thanks for correcting me.
  • Heater.Heater. Posts: 21,230
    edited 2010-10-19 07:33
    pgbpsu,
    I don't know how I made something so simple so complicated.

    Excellent. Don't worry. I've travelled that road many times and probably will again.
  • max72max72 Posts: 1,155
    edited 2010-10-19 12:58
    check also the number to string objects

    numbers
    and
    simple numbers

    this way you can convert any number in any format.

    Massimo
Sign In or Register to comment.