LabWindows/CVI

cancel
Showing results for 
Search instead for 
Did you mean: 

sfd

  I'm somewhat frustrated. I have an array(not known size) like a[0]=0x33;a[1]=0x44;a[3]=0x55. I want to format it a whole string like "33 44 55". What I did was to build a subfunction, so when I need to do that, I can use DataLogMEM(dataSource,size, dataResult).
  The problem that frustrated me is that I always have the issue of "Attempt to write beyond end of string" when trying to run it. How can I resolve this kind of problem?
 
Thanks!
Jacky
 
 
----------------------------------------------------
int DataLogMEM(unsigned char *dataSource,int size, unsigned char *dataResult)
{
 unsigned char bb[4];
 int i=0;
 
 for (i=0;i<size;i++)
    {
     sprintf(bb,"%0.2x ",dataSource[i]);
  strcat(dataResult,bb);
 }
0 Kudos
Message 1 of 2
(3,757 Views)

You need to revise exactly the dimension of your strings: given this error either bb is too short for some of the elements to format into or the destination string cannot hold all substrings appended to it.

You could also try excluding the use of an intermediate string to format data, for example using sprintf (dataResult, "%s %0.2x", dataResult, dataSource[i]); dataResult string needs to be inizialized to an empty string at the beginning of the function.

Also, when the error arise, you could examine in the Variables window the content of i, bb and dataResult to discover what's going on.

And don't forget that you could also use Fmt functions, that accepts variable-lenght arrays when formatting, using asterisks instead of repetition code and passing the size of the array as one of its arguments. This subject is extensively discussed in the Formatting and I/O library help.



Proud to use LW/CVI from 3.1 on.

My contributions to the Developer Community
________________________________________
If I have helped you, why not giving me a kudos?
0 Kudos
Message 2 of 2
(3,747 Views)