I need help merging strings.
Richard McCue
Posts: 6
I am trying to generate unique file names to save to a SD card. My intention is to merge the cycle counter with a string that has the device serial number and the file extension. I am using the Numbers_to_string.spin object and I am having trouble storing the string that is returned from it. I want to be able to write file names to the SD card with a·XXXXFDR1.xls file name (XXXX is an incremental cycle counter, and FDR1 is a serial number.) Any help would be greatly appreciated. I am still somewhat of a novice.
OBJ
··
· debug: "Parallax serial terminal"··
· sd:··· "fsrwFemto"············· ''SD Software used in FemtoBASIC
· num:·· "Numbers_To_String"
VAR
· word cycle 'temp var
· long ioControl[noparse][[/noparse]2]·· ''** Variable required for SD Routines **···
· byte buff[noparse][[/noparse]32]······ '' buffer for SD
· byte temp[noparse][[/noparse]4]······ ' temp buf
dat
· Filename byte "··· FDR1.xls", 0··· ' Serial Number and File Extension
·
PUB Main | idx
· cycle := 1·················· ·'· cycle counter
· debug.start(230400)····· ·' PST·········
· waitcnt(clkfreq*4 + cnt)·········
· debug.clear
·
· repeat 10
··· byte[noparse][[/noparse]temp] := num.tostr(cycle, num#dec)······· ' Save cycle counter string to Temporary Variable
··· debug.str(num.tostr(cycle, num#dec))
··· idx := 0
····· repeat while temp > 0······················· ' Repeat until Zero terminate
······· byte[noparse][[/noparse]@filename][noparse][[/noparse]idx] := byte[noparse][[/noparse]temp][noparse][[/noparse]idx++]· ' add cycle counter to front of Serial number and file extension.
··· debug.str(@filename)·························· ' display complete file name
··· debug.newline
··· cycle++·································· ' Increment the Cycle counter
OBJ
··
· debug: "Parallax serial terminal"··
· sd:··· "fsrwFemto"············· ''SD Software used in FemtoBASIC
· num:·· "Numbers_To_String"
VAR
· word cycle 'temp var
· long ioControl[noparse][[/noparse]2]·· ''** Variable required for SD Routines **···
· byte buff[noparse][[/noparse]32]······ '' buffer for SD
· byte temp[noparse][[/noparse]4]······ ' temp buf
dat
· Filename byte "··· FDR1.xls", 0··· ' Serial Number and File Extension
·
PUB Main | idx
· cycle := 1·················· ·'· cycle counter
· debug.start(230400)····· ·' PST·········
· waitcnt(clkfreq*4 + cnt)·········
· debug.clear
·
· repeat 10
··· byte[noparse][[/noparse]temp] := num.tostr(cycle, num#dec)······· ' Save cycle counter string to Temporary Variable
··· debug.str(num.tostr(cycle, num#dec))
··· idx := 0
····· repeat while temp > 0······················· ' Repeat until Zero terminate
······· byte[noparse][[/noparse]@filename][noparse][[/noparse]idx] := byte[noparse][[/noparse]temp][noparse][[/noparse]idx++]· ' add cycle counter to front of Serial number and file extension.
··· debug.str(@filename)·························· ' display complete file name
··· debug.newline
··· cycle++·································· ' Increment the Cycle counter
Comments
Then you can use:
bytefill( @filename, 0, 13)
bytemove( @filename, string( "FDR" ), 3 )
bytemove( @filename+3, num.tostr( cycle, num#dec ), 5 )
bytemove( @filename+strsize(@filename), string( ".xls" ), 5 )
Now the explaination:
bytefill makes sure that the whole string has stringend characters at the beginning.
The first bytemove copies the fixed beginning of the filename to the beginning of the filename.
num.tostr can not return a string, it only contains the pointer to a string. So you have to copy the string to wherever you want it. And you want it directly after "FDR", so you copy it strating at address of filename plus 3 characters. The buffer returned by tostr contains a string, which definitely will have a string end character. So we can simply copy the max. number of bytes that are allowed for a filename - the 3 characters already used for "FDR" = 5.
As long as cycle is below 100000 you don't have problems with that. If cycle is above 99999 it will overwrite a file that might exist already.
The last bytemove simply appends the extension overwriting the stringend of the previous bytemove and including it's own stringend.
PS: and now I read the text above your code ;o) ... ok ... filename should look different, so the right code
would be:
bytefill( @filename, 0, 13)
bytemove( @filename, num.tostr( cycle, num#dec ), 4 )
bytemove( @filename + strsize( @filename ), string( "FDR1.XLS" ), 9 )
Post Edited (MagIO2) : 4/29/2010 9:04:28 PM GMT