Click to See Complete Forum and Search --> : primitive data question


ideru
February 23rd, 2006, 08:34 PM
hello :wave:

too much use of CString and now I don't know how to use unsigned char anymore....

just some question/confirmation

unsigned char data[128];

1. how to put data? memcpy?
ex. memcpy(data, "the quick brown fox", 128 ) ---> ??

2. how to know the actual data length of the data variable??

wildfrog
February 23rd, 2006, 08:45 PM
Maybe this helps.

#include <memory.h>
#include <string.h>

unsigned char data[128];

int _tmain(int argc, _TCHAR* argv[])
{
// using memcpy (be careful with the length, string length + ending zero)
memcpy(data, "the quick brown fox", 20);

// using strcpy (takes signed char*, not unsigned)
strcpy((char*) data, "the quick brown fox");

// using sprintf (takes signed char*, not unsigned)
sprintf((char*) data, "the quick brown fox");

// get size of data
int datasize = sizeof(data);

// get length of string contained within data (takes signed char*, not unsigned)
int stringsize = strlen((char*) data);

return 0;
}

- petter

ideru
February 23rd, 2006, 08:50 PM
thank you very much :wave::thumb:

RoboTact
February 24th, 2006, 08:41 AM
hello :wave:

too much use of CString and now I don't know how to use unsigned char anymore....

just some question/confirmation

unsigned char data[128];

1. how to put data? memcpy?
ex. memcpy(data, "the quick brown fox", 128 ) ---> ??

2. how to know the actual data length of the data variable??What is the context? What about that CString/unsigned char issue?

kenrus
February 24th, 2006, 10:36 AM
2. how to know the actual data length of the data variable??

wildfrog's example is correct, but be careful of the common mistake ...



int EvalStr(char* data);

int main(int argc, char** argv)
{
char data[128];

EvalStr(data);

return 0;
}

int EvalStr(char* data)
{
int i = 0;

i = sizeof(data); /*returns sizeof char *, not allocated size of data*/

strncpy(data, "My useful text information", i-1);

return 0;
}



-Kendall