Shop OBEX P1 Docs P2 Docs Learn Events
Help with data type conversion — Parallax Forums

Help with data type conversion

thetribsterthetribster Posts: 11
edited 2009-06-04 16:26 in Propeller 1
I am new to this forum and to SPIN, interesting language.

Anyway, I have a string:

tel.str(string("/WB3Day.php?z=33051:"))

and an a Long named pagenumber. I'd like to append the pagenumber to the tel.str - just after the 33051: so it would look like this:

/WB3Day.php?z=33051:1 (assuming pagenumber = 1).

I'm having difficulty with the language, hopefully the experienced devs here can help. Thanks in advance.

Comments

  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-03 01:28
    The easiest thing to do is use a call to tel.dec like this:

    tel.str(string("/WB3Day.php?z=33051:"))
    tel.dec(pagenumber)
  • thetribsterthetribster Posts: 11
    edited 2009-06-03 13:03
    Beautiful, I'll try that. Thanks Mike.
  • thetribsterthetribster Posts: 11
    edited 2009-06-03 15:15
    Yet another question, BTW the tel.dec worked great. Now I need to append a hex number ot the string before transmission, the code follows:

    tel.str(@path_holder)
    tel.dec(pagenumber)
    tel.str(string(":"))
    if settings.getData(settings#NET_MAC_ADDR,@infostack,6) ' Grab the MAC address, display it
    tel.str(string("MAC: "))
    repeat i from 0 to 5
    if i
    tel.str(string("-"))
    tel.hex(byte[noparse][[/noparse]@infostack],2)

    The path_holder value is: "/IW.php?Dave:" I then append the pagenumber on. I would now like to add the mac address - the IF construct is meant to handle it but all I get is '-' and not the address. Help [noparse]:)[/noparse]
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-03 15:25
    When you want to post code snippets, use [noparse][[/noparse] code ] and [noparse][[/noparse] /code ] brackets around the code (without the spaces in the square brackets). This will keep most of the formatting intact. You also need to put spaces in your subscripts. The square brackets are used for other formatting commands. For example, [noparse][[/noparse] i ] without the spaces specifies italics.

    How is infostack defined?
  • thetribsterthetribster Posts: 11
    edited 2009-06-03 15:50
    Sorry, didn't realize - I'll do that.

    The infostack is defined as a long[noparse][[/noparse]200].
    The pathholder variable is Byte[noparse][[/noparse]128].

    Thx for the quick responses!

    Here is code that takes the MAC addrress out of memory and writes it to the screen (this works):

    [noparse][[/noparse] code ]
    if settings.getData(settings#NET_MAC_ADDR,@infostack,6) ' Grab the MAC address, display it
    term.str(string("MAC: "))
    repeat i from 0 to 5
    if i
    term.out("-")
    term.hex(byte[noparse][[/noparse]@infostack],2)
    term.out(13)
    [noparse][[/noparse] /code ]

    Here is what I am currently doing that is not working:

    [noparse][[/noparse] code ]
    case pagenumber
    0 :
    tel.str(@path_holder)
    tel.dec(pagenumber)
    tel.str(string(":"))
    if settings.getData(settings#NET_MAC_ADDR,@infostack,6) ' Grab the MAC address
    tel.str(string("MAC: "))
    repeat i from 0 to 5
    if i
    tel.str(string("-"))
    tel.hex(byte[noparse][[/noparse]@infostack],2)
    tel.str(string("/BC.php?p="))
    [noparse][[/noparse] /code ]
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-03 16:00
    Please re-post your message with the [noparse][[/noparse] code ] brackets without any spaces inside the square brackets.
  • thetribsterthetribster Posts: 11
    edited 2009-06-03 19:53
    These are the two pieces of code - the first displays the data I need to the screen, the second doesn't work - it's mission is to append the MAC address onto the @path_holder string.

     if settings.getData(settings#NET_MAC_ADDR,@infostack,6)                       ' Grab the MAC address, display it
        term.str(string("MAC: "))
        repeat i from 0 to 5
          if i
            term.out("-")
          term.hex(byte[noparse][[/noparse]@infostack][i],2)
        term.out(13)  
    [/i]
    



          0   :
            tel.str(@path_holder)
            tel.dec(pagenumber)
            tel.str(string(":"))
            if settings.getData(settings#NET_MAC_ADDR,@infostack,6)                       ' Grab the MAC address, display it
              tel.str(string("MAC: "))
              repeat i from 0 to 5
                if i
                  tel.str(string("-"))
                tel.hex(byte[noparse][[/noparse]@infostack][i],2)
    [/i]
    
  • mparkmpark Posts: 1,305
    edited 2009-06-03 20:20
    It's confusing I know, but you take the spaces *out* of the [noparse][[/noparse] code ] tags and you put spaces *into* subscripts ( [noparse][[/noparse] i ] ).

    Also, preview is your friend.
  • thetribsterthetribster Posts: 11
    edited 2009-06-03 20:48
    Thx.

    BTW, the path_holder value just before the pagenumber concatenation is:

    /IW_0.php?p=33051:

    then the pagenumber is added /IW_0.php?p=33051:1 - this works (thanks to Mike). Now, I'd like to add another colon (easy enough) and the MAC string - so the result looks something like this:

    /IW_0.php?p=33051:1:FFDDFF0966FF

    the second widget of code (in the earlier post) is where I am having the problem.
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-03 20:54
    After the "tel.dec()", you'd put

    tel.out(":")
    repeat i from 0 to 5
       tel.hex(infostack.byte[noparse][[/noparse] i ],2)
    
  • thetribsterthetribster Posts: 11
    edited 2009-06-03 22:22
    Hey Mike, thanks. Almost worked, I had to replace the tel.out with tel.str but everything compiles and appears to work except that the server is only seeing the "Dave:" part of the parameters (this is derived from eeprom settings). The problem is (I think) that the items (pagenumber and mac addr) need to concatenated into the string as opposed to just tacked on the end - my mistake, should have realized that as the string is used later in a function call (actually several).

    This is the original InfoWidget for the yBox that I've transformed into a generic RSS reader (and a little more) - just an FYI in case you know anything about the yBox.

    Anyway...What I need to be able to do is append ":" + pagenumber + ":" + macaddr to the existing path_holder string (or create a new one). I am a funbling idiot with SPIN.

    Thanks Mike, your help and responsiveness are appreciated - very much - I'm way better at writing PHP/PERL code [noparse]:)[/noparse]
  • Mike GreenMike Green Posts: 23,101
    edited 2009-06-03 23:04
    Try this object. You declare a byte array and pass the address and length of the array to the start method of this object. It has the usual methods used for displaying or printing values and these are converted into strings and catenated to the end of the string you're building. Anything beyond the end of the declared array is ignored. To reinitialize the string (array), just call the start method again. In your case, you'd do something like:
    OBJ build : "StringIO"
    
    VAR byte strArray[noparse][[/noparse]80]
    
    build.start(@strArray,80)
    build.str(string("/IW_0.php?p=33051:"))
    build.dec(pagenumber)
    build.out(" ")
    repeat i from 0 to 5
       build.hex(infostack.byte[noparse][[/noparse] i ],2)
    ' Now the complete string is in strArray
    
  • thetribsterthetribster Posts: 11
    edited 2009-06-04 16:26
    Thanks Mike, that worked beautifully after a couple adjustments.

    First, I got a compile error on the function name 'length' so I changed to lgth.

    Secondly, while the infostack is being passed in the main routine it isn't populated with the right data - i.e. the mac addr. I added a line to hydrate with the mac addr before calling your build rouint.

    The code is below. Now that I can do this dynamically (tell the prop the next page to call) from the server side I can eliminate the case construct...

    The is very cool for me, a major item is now done and I can focus on the server side 100%.

    Mike, I can't thank you enough for your help through this - wish I could do something for you.

    Also, I noticed your StringIO routines are being downloaded by others smile.gif

    Here is the current !working! code.

        case pagenumber
          0   :
            if settings.getData(settings#NET_MAC_ADDR,@infostack,6)      
              build.start(@path_holder,64)
              build.str(string("/IW.php?p="))
              build.dec(pagenumber)
              build.out(":")
              repeat i from 0 to 5
                build.hex(infostack.byte[noparse][[/noparse] i ],2)
              tel.str (@path_holder)
    
    
Sign In or Register to comment.