Shop OBEX P1 Docs P2 Docs Learn Events
Help with replacing part of a "string" — Parallax Forums

Help with replacing part of a "string"

eagletalontimeagletalontim Posts: 1,399
edited 2014-11-28 21:10 in Propeller 1
Hello all again! I am attempting to modify a DAT variable before it is sent to the server, but I can't seem to get the DATAHERE to replace with a number that is calculated.
CON
  _clkmode = xtal1 + pll16x     
  _xinfreq = 5_000_000

  CR            = $0D
  LF            = $0A

DAT
  DataSeg1      byte  "GET /tims/post.php?data=DATAHERE HTTP/1.1", CR, LF, {
}                     "Host: www.example.com", CR, LF, {
}                     "User-Agent: Wiz5100", CR, LF, CR, LF, $0

OBJ
  str             : "STRINGS2"
  pst             : "Parallax Serial Terminal"

VAR
  byte str_test[255]
  byte new_str[255]
  byte value

PUB Main | strh

  pst.Start(115_200)      
  pause(5000)

  value := (12345 + 12345) / 2

  strh := @DataSeg1
  bytefill(@str_test, 0, 255)
  bytemove(@str_test, strh, strsize(strh))
  strh := str.StrReplace(@str_test, string("DATAHERE"), value)  ' *******  Lost Here????
  pst.str(strh)

PRI pause(Duration)  
  waitcnt(((clkfreq / 1_000 * Duration - 3932) #> 381) + cnt)
  return 

I can replace the line in question with :
strh := str.StrReplace(@str_test, string("DATAHERE"), string("12345"))
and it works exactly as needed.

How can I replace the DATAHERE with a calculated number?

Comments

  • SRLMSRLM Posts: 5,045
    edited 2014-11-28 18:37
    Can't you construct the number as a string (in a "scratchpad" area), then use StrReplace with the calculated string?
  • eagletalontimeagletalontim Posts: 1,399
    edited 2014-11-28 18:46
    Not sure I quite understand. The number that will be calculated is the pulse width of a flashing IR LED which will change with every pulse. How could I construct the number as a string?
  • Cluso99Cluso99 Posts: 18,069
    edited 2014-11-28 18:58
    Kye wrote a good set of string handling routines. IIRC they are in the OBEX and are worth looking at, as well as using them.
  • localrogerlocalroger Posts: 3,451
    edited 2014-11-28 19:08
    If you want to output the number as ASCII text, you have to convert it to a string of ASCII characters. The typical Propeller libraries don't do this very well; instead of having generic string functions they have methods like "dec" built into every output driver.

    I have written a library that does what you need. It didn't get a lot of attention but you can find it here:

    http://forums.parallax.com/showthread.php/148937-Concept-Test-General-Purpose-String-Output-Formatting
  • eagletalontimeagletalontim Posts: 1,399
    edited 2014-11-28 19:09
    Yep, that was it. Just found it. I knew there was a function somewhere. Already had it, just needed to use it.
  • SRLMSRLM Posts: 5,045
    edited 2014-11-28 19:13
    You want to insert the decimal representation of a variable into a string, right? Then the source of the number doesn't really matter. All you need is a variable that has the number, and a function to convert that into a string. Such can be found, for example, here, under integer to decimal:
    PUB integerToDecimal(number, length) '' 5 Stack Longs
    
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    '' // Converts an integer number to the decimal string of that number padded with zeros.
    '' //
    '' // Returns a pointer to the converted string.
    '' //
    '' // Number - A 32 bit signed integer number to be converted to a string.
    '' // Length - The length of the converted string, "+" or "-" will be concatenated onto the head of converted string.
    '' ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
    
      length := (10 - ((length <# 10) #> 0))
    
      decimalString := "+"
      if(number < 0)
        decimalString := "-"
    
      if(number == negx)
        bytemove(@decimalString, string("-2147483648KA"), 11)
    
      else
        repeat result from 10 to 1
          decimalString[result] := ((||(number // 10)) + "0")
          number /= 10
    
      decimalString[length] := decimalString
      return @decimalString[length]
    


    FDS+ also has a decimal to string function, although you'd have to modify it to write to a buffer instead of the tx() function.
    PUB dec(value) | i, x
    
    '' Print a decimal number
    
      x := value == NEGX                                                            'Check for max negative
      if value < 0
        value := ||(value+x)                                                        'If negative, make positive; adjust for max negative
        tx("-")                                                                     'and output sign
    
      i := 1_000_000_000                                                            'Initialize divisor
    
      repeat 10                                                                     'Loop for 10 digits
        if value => i                                                               
          tx(value / i + "0" + x*(i == 1))                                          'If non-zero digit, output digit; adjust for max negative
          value //= i                                                               'and digit from value
          result~~                                                                  'flag non-zero found
        elseif result or i == 1
          tx("0")                                                                   'If zero digit (or only digit) output it
        i /= 10                                                                     'Update divisor
    
  • Phil Pilgrim (PhiPi)Phil Pilgrim (PhiPi) Posts: 23,514
    edited 2014-11-28 21:10
    Chip's SimpleNumbers object that's part of the Spin library will do what you want.

    -Phil
Sign In or Register to comment.