Shop OBEX P1 Docs P2 Docs Learn Events
Help with strings/hex? [solved] — Parallax Forums

Help with strings/hex? [solved]

teddypteddyp Posts: 14
edited 2010-04-20 09:45 in Propeller 1
So, what I have is the date in the format MMDDYY in string format. I want to print it as MM/DD/YY to the serial terminal. I have tried string and string2 with horrible amounts of fail. If I have the date in a variable called "date" how can I format this? Using some code...
    pst.str(date)           'prints out the date 041910 for today
    pst.str(string(13))   'CR
    pst.dec(strsize(date))   'prints out decimal 6 (length of the date string)
    pst.str(string(13))    'CR
    repeat k from 0 to strsize(date) - 1     'from 0 to 5 
      pst.hex(byte[noparse][[/noparse]date][noparse][[/noparse]k],2)                  'I get 30 34 31 39 31 30 all hex good.
      pst.str(string(13))



but, if I try to use the terminal and...
    pst.str(byte[noparse][[/noparse]date][noparse][[/noparse]0])
    pst.str(byte[noparse][[/noparse]date][noparse][[/noparse] 1 ])



I get crazy characters on the screen
I tried to create a temp string. from what I see in memory date is preceded and superseded with 00 or EOT. so, if I...
    byte[noparse][[/noparse]@MyDate][noparse][[/noparse]0] :=$00
    byte[noparse][[/noparse]@MyDate][noparse][[/noparse] 1 ] := byte[noparse][[/noparse]date][noparse][[/noparse]0]
    byte[noparse][[/noparse]@MyDate][noparse][[/noparse] 2 ] := byte[noparse][[/noparse]date]
    byte[noparse][[/noparse]@MyDate][noparse][[/noparse] 3 ] := $00
    pst.hex(MyDate,8)



I get all 0's when I print it out to the terminal and crazyness if I tried to print the string. What am I doing wrong here? Why am I having such a hard time with something so simple? Thanks in advance anyone.

Post Edited (teddyp) : 4/19/2010 10:04:42 PM GMT

Comments

  • MagIO2MagIO2 Posts: 2,243
    edited 2010-04-19 21:33
    How about

    repeat i from 0 to 5
      pst.tx( byte[noparse][[/noparse] @date ][noparse][[/noparse] i ] )
      if i&1
        pst.tx( "/" )
    

    The problem with your code is that you call pst.str without giving an address. str function want's an address and not values. And the address must point to a byte array which has a $00 as a string-end character.

    The tx-function is fine with the character value, so you can use this to print exactly one character.

    Post Edited (MagIO2) : 4/19/2010 9:40:06 PM GMT
  • teddypteddyp Posts: 14
    edited 2010-04-19 22:02
    Well, I couldnt find the tx command in the propeller serial terminal. At any rate, I totally understand what you mean now. I found the propeller wiki talking about strings and the bytemove command. I got the data where I can use it playing with this code..
    MyDate := string("ABC")
      repeat                                               'Repeat forever
        pst.str(date)                 'gives me 041910 for today
        pst.str(string(13))         'CR
        
        pst.dec(strsize(date))   'print size of string (is 6 for this)
        pst.str(string(13))         'CR
        repeat k from 0 to strsize(date) - 1  'Repeat from 0 to 5 
          pst.hex(byte[noparse][[/noparse]date][noparse][[/noparse]k],2)               'Print hex value for each char
          pst.str(string(13))                       'CR
    
        pst.str(MyDate)                         'print MyDate (is still ABC from above)
        pst.str(string(13))                       'CR
        bytemove(MyDate, date, 2)         'Move first two bytes of date into MyDate
        pst.str(MyDate)                           'print MyDate (is now 04C in the terminal)
        pst.str(string(13))                       'CR
        bytemove(MyDate, date+2, 2)    'took me a minute to figure this one out. It's not the location with an offset as
                                                       'in date  it's the ADDRESS + 2. 
        pst.str(MyDate)                          'Prints 19C to term
        pst.str(string(13))                        'CR
        bytemove(MyDate, date+4, 2)    'You get the picture
        pst.str(MyDate)
        pst.str(string(13))
    
    



    Thanks again for the help! I wish I would've found that wiki earlier
  • MagIO2MagIO2 Posts: 2,243
    edited 2010-04-20 09:45
    Be carefull. Now you overwrite memory with your date. Originally MyDate contains an address pointing to a memory area which holds the bytes "A", "B", "C" and 0 (which is the string terminator). So, all in all 4 bytes are reserved for the string. This memory area can be placed anywhere in memory ... the compiler will do that. But whatever comes next will be overwritten if you copy the full dat to that location.

    Simple_Serial and FullDuplexSerial both have the function tx and I'm pretty sure that any serial interface has something similar. What exactly is PST?
Sign In or Register to comment.