-
October 19th, 2007, 07:25 AM
#1
Copying from unsigned char * to the CString
Hi All,
I have unsigned char * buffer in which some data are stored , now I want to move these all data to my CString variable for some operations.
I tried to copy but problem is that in my unsigned char * some data are padded as zero, So I hope that in CString it gets the null value so it can not copy whole data.
I tried to copy from unsigned char * to the CString using...
CString tmpMsg = CString(msg); // Here msg is the unsigned char *
Ashish
-
October 19th, 2007, 07:34 AM
#2
Re: Copying from unsigned char * to the CString
Don't try to use CString for containing binary data - it won't work. Tell us more about what you are doing and we can suggest alternatives.
-
October 19th, 2007, 07:35 AM
#3
Re: Copying from unsigned char * to the CString
So you want to copy a buffer to a CString variable and want to keep the characters that are set to 0. So that the string was something like this 'ABC\0\0DEF'. Is that correct?
-
October 19th, 2007, 07:41 AM
#4
Re: Copying from unsigned char * to the CString
In my unsigned char * buffer I have all char data, so I want all these data into my CString variable.
I tried to use memcpy() ,but it gives me the runtime error like....
TestRTMP.exe: 0xC0000005: Access violation reading location 0x49468964.
so plz help me.
Thanks.
-
October 19th, 2007, 07:45 AM
#5
Re: Copying from unsigned char * to the CString
Originally Posted by cilu
So you want to copy a buffer to a CString variable and want to keep the characters that are set to 0. So that the string was something like this 'ABC\0\0DEF'. Is that correct?
when 0 value comes in CString it shows nothig(Just like blank space)...
I m not getting that to much,but the problem is of zero value.
-
October 19th, 2007, 07:55 AM
#6
Re: Copying from unsigned char * to the CString
While it is technically possible to store a binary 0 in a CString you should not do it. There are too many problems that will be encountered. The bottom line is that most of the CString member functions (including the constructor you are trying to use) assume binary 0 is the end-of-string terminator and will fail for whatever purpose you intend. Once again, you need to re-think your application and use a container other than CString.
-
October 19th, 2007, 09:00 AM
#7
Re: Copying from unsigned char * to the CString
0xC0000005 is right, my suggestion is to use std::string for that purpose.
Regards,
Usman.
-
October 19th, 2007, 09:05 AM
#8
Re: Copying from unsigned char * to the CString
Although it's not a very brilliant idea, you can store any character including '\0' in a CString object as follows
Code:
void CFoo::FillString(CString& str, unsigned char* msg, int length)
{
char* buffer = str.GetBufferSetLength(length);
memcpy(buffer, msg, length);
str.ReleaseBuffer(length);
}
With one condition: use that object only for storing; another subsequent call of one of CString methods may truncate it to the first '\0' value.
Last edited by ovidiucucu; October 19th, 2007 at 09:11 AM.
-
October 19th, 2007, 09:07 AM
#9
Re: Copying from unsigned char * to the CString
Originally Posted by usman999_1
0xC0000005 is right, my suggestion is to use std::string for that purpose.
UUUghhh!
-
October 19th, 2007, 09:13 AM
#10
Re: Copying from unsigned char * to the CString
Originally Posted by ovidiucucu
UUUghhh!
-
October 19th, 2007, 09:18 AM
#11
Re: Copying from unsigned char * to the CString
I wouldn't use any kind of string, std, CString or anything else you find to store data that contains a NULL.
-
October 19th, 2007, 09:22 AM
#12
Re: Copying from unsigned char * to the CString
Right.
A possible better approach is to write your own "buffer" class like for example:
Code:
class CBuffer
{
unsigned char* buffer;
unsigned int length;
public:
CBuffer() : buffer(NULL), length(0) {};
SetBuffer(unsigned char* src, unsigned int length);
unsigned int GetLength() const {return length;}
// and so on, and so on... all you need
};
-
October 19th, 2007, 09:23 AM
#13
Re: Copying from unsigned char * to the CString
Originally Posted by GCDEF
I wouldn't use any kind of string, std, CString or anything else you find to store data that contains a NULL.
What's wrong with std::string???
-
October 19th, 2007, 09:25 AM
#14
Re: Copying from unsigned char * to the CString
Originally Posted by usman999_1
What's wrong with std::string???
Why do you think it's better than CString?
-
October 19th, 2007, 09:29 AM
#15
Re: Copying from unsigned char * to the CString
What is the advantage of writing your own buffer class instead
of using std::string ???????????????????????
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
|