Shop OBEX P1 Docs P2 Docs Learn Events
Need some help trying to build a string — Parallax Forums

Need some help trying to build a string

Don MDon M Posts: 1,652
edited 2014-02-09 00:54 in Propeller 1
What I'm trying to do is make a file name for SD card based on month/day/hour.

The file name would look like this: mmddhh.txt So for todays date at 1600 hours would be 020816.txt

I can get the month / date / hour from the clock object like this using todays date:
month := clock.GetMonth
date := clock.GetDay
hour := clock.GetHour

DecN is a method for setting the number of digits.

If I use term.decn(month,2) I get 02. Term.decn(date,2) I get 08. Term.decn(hour,2) I get 16.

So how do I build a string using the above?

I tried declaring a word buffer called FileName[3] and using it like this:
FileName[2] := clock.GetMonth
FileName[1] := clock.GetDay
FileName[0] := clock.GetHour

Then tried printing it out like this: term.decn(@FileName,6) but I get a result of 012496 which makes no sense to me.

Can someone help me out with this?

Thanks.

Comments

  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-02-08 15:39
    You want a method like decn to insert the characters within your string.

    There's an object called StrFmt posted by LocalRoger with has a method that will do this.

    The method is "fdec".

    StrFmt is included in the archive of my MAX7219 code.

    I'll look for an example of using of the fdec method and post it soon.
  • Duane DegnDuane Degn Posts: 10,588
    edited 2014-02-08 15:50
    Assuming Format is the object name of "StrFmt".

    The following code should load the string with the time stamp.
    month := clock.GetMonth
    date := clock.GetDay
    hour := clock.GetHour
    
    result := @FileName ' "result" could be any temporary variable at least a word in size
    result := Format.fdec(result, month, 2, -2)
    result := Format.fdec(result, date, 2, -2)
    result := Format.fdec(result, hour, 2, -2)
    term.str(@FileName) ' this should display the time stamped file name.
    DAT
    FileName    btye "000000.txt", 0
    
    
  • Don MDon M Posts: 1,652
    edited 2014-02-08 16:19
    Hmmm... Ok Duane you got me thinking here...

    I found some other code for numbering files on SD card that Jonny Mac showed on here some time ago.
    pub make_nfilename(n, spntr, pos)
    
    '' Adds 2 digit # n to string at spntr
    '' -- pos is position of 10's digit in string
    
      n := 0 #> n <# 99
    
      byte[spntr][pos+0] := n / 10 + "0"
      byte[spntr][pos+1] := n // 10 + "0"
      
    

    I used that in conjuction with this that I made and it works:
    pub make_filename
    
    '' Makes file name using date and hour in this format: mmddhh.txt
    
      make_nfilename(clock.GetMonth, @FileName, 0)
      make_nfilename(clock.GetDay, @FileName, 2)
      make_nfilename(clock.GetHour, @FileName, 4)
    
      term.str(@FileName)
    
    dat
    
    FileName byte "000000.TXT", 0  
    
    

    So thanks for your help and maybe someone else can use this.

    Thanks again!
    Don
  • PropGuy2PropGuy2 Posts: 360
    edited 2014-02-08 18:41
    Here Is the method I use for string formats, use the bytemove commad:

    bytemove( @strDATE, string("00.00.0000"),11) 'Setup strDATE template
    n1 := SN_.decx(n2, 2) 'Put mm dd yyyy in string @strDATE
    bytemove( @strDATE.byte[0], n1, 2 ) 'use simple_numbers: integer to string Date
    d1 := SN_.decx(d2, 2) 'as n2>n1, d2>d1 and y2+2000>y1
    bytemove( @strDATE.byte[3], d1, 2 ) 'Must keep absolute Index to work
    y1 := SN_.decx(y2 +2000, 4) 'Note yyyy is4 places in the bytemove string
    bytemove( @strDATE.byte[6], y1, 4 )
    --- --- --- ---
    Print.str( @strDATE ) 'Print finished @strDATE
  • StefanL38StefanL38 Posts: 2,292
    edited 2014-02-09 00:54
    Hi PropGuy,

    bytemove works well. But you have to be very careful about making sure moving the values to the right adress.
    If something runs wrong there your code will behave very strange.

    best regards

    Stefan
Sign In or Register to comment.