Re: primitive data question
Maybe this helps.
Code:
#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
Re: primitive data question
thank you very much :wave::thumb:
Re: primitive data question
Quote:
Originally Posted by ideru
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?
Re: primitive data question
Quote:
Originally Posted by ideru
2. how to know the actual data length of the data variable??
wildfrog's example is correct, but be careful of the common mistake ...
Code:
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