|
-
February 23rd, 2006, 09:34 PM
#1
primitive data question
hello 
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??
-
February 23rd, 2006, 09:45 PM
#2
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
-
February 23rd, 2006, 09:50 PM
#3
Re: primitive data question
thank you very much 
-
February 24th, 2006, 09:41 AM
#4
Re: primitive data question
 Originally Posted by ideru
hello
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?
"Programs must be written for people to read, and only incidentally for machines to execute."
-
February 24th, 2006, 11:36 AM
#5
Re: primitive data question
 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
John 3:16
For God so loved the world ...
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|