Shop OBEX P1 Docs P2 Docs Learn Events
Spin2 Concatenate question — Parallax Forums

Spin2 Concatenate question

Maybe I missed it - But I am trying to Concatenate / Join, two sets of Integer numbers, and alternately two sets of Strings into one value:

For instance: Integer number = 50, AND an Integer number = 34567, into a single Integer value = 5034567

Alternately: String value = "John", AND a string value = "Smith" - into a single string = "John Smith"

Is there a Spin2 function - or has anyone written a function to do this ???

Comments

  • Spin has no string type, nor any built-in text/integer conversions. You'll need byte arrays to store your strings. Here's an example of concatenating two strings (passed as pointers).

    pub concat(p_str1, p_str2, p_out) : p_str
    
    '' Concatenates strings p_str1 and p_str2
    '' -- p_out is pointer to separate output buffer
    ''    * allows preservation of p_str1 and p_str2
    ''    * do not use p_str2 as the output buffer
    
      if (p_out <> p_str1)
        bytemove(p_out, p_str1, strsize(p_str1)+1)                  ' move s1 with terminator
      bytemove(p_out + strsize(p_out), p_str2, strsize(p_str2)+1)   ' move s2 with terminator
    
      return p_out                                                  ' return pointer to result
    

    Have a look at the attached demo -- it does the things you want to do. The key is understanding that there is no string type, that you need a byte array that is as big as your longest string plus one, and that there needs to be a zero following the last character of your string.

  • PropGuy2PropGuy2 Posts: 358
    edited 2022-08-17 23:12

    Thanks to Jon for the code ConCat Demo. I gently edited and added labels for clarity.

    Hint - Make a new Directory for the ConCat Demo, and use PropTool 2.7.0 Beta

    Hope this helps for anyone interested, Enjoy.

    See the new main ConCat code attachment, the balance of code functions remains the same.

  • Here's a method (untested) for concatenating two integers that doesn't require conversion to strings and back. The integers must be positive.

    PUB catint(a, b) | mult
      mult := 10
      repeat until (mult > b)
        mult *= 10
      return a * mult + b
    
  • Francis BauerFrancis Bauer Posts: 353
    edited 2022-08-19 01:12

    @"Phil Pilgrim (PhiPi)" said:
    Here's a method (untested) for concatenating two integers that doesn't require conversion to strings and back. The integers must be positive.

    PUB catint(a, b) : value | mult
      mult := 10
      repeat until (mult > b)
        mult *= 10
      return a * mult + b
    

    Phil's example appears to work fine using PropTool 2.7.0, you just need to add ": value" to the PUB definition for the routine to return a value.

Sign In or Register to comment.