How can convert a char array to CString?
Hi All,
How can i convert a char array into CString?
I have one array like this
char charr[1000];
....
drwFile.Read(charr,500); //reading some characters from the file
CString str;
how to store the charr array in to str?
i m try this CString str(charr);
but when i display a message box with str value its display only first character of charr.
please help me for this.
thanks in advance.
Re: How can convert a char array to CString?
1. Is your build a UNICODE one?
2. Is the file you are reading in a UNICODE one?
Besides: Which compiler/IDE are you using?
Re: How can convert a char array to CString?
Your MFC application has a built-in switch between double-byte characters (called UNICODE from MS) and single-byte characters (called MULTIBYTE by MS because some characters only can be expressed by more than one byte).
If we assume your project is UNICODE now (default) the CString would accept only double-byte characters. So, assigning a char array (single-bytes) mostly leads to non-printable characters.
To convert the char array you may using the following code
Code:
CString str;
if (sizeof(TCHAR) == 1)
str = charr;
else
{
mbstowcs(str.GetBuffer(500), charr, 500);
str.ReleaseBuffer(-1);
}
In both cases the charr must be zero-terminated what best is achieved by initializing the array with zeros before use.
Code:
char charr[500] 0 { '\0' }; // all zeros
Re: How can convert a char array to CString?
Just use CString's = operator. It will handle the conversion:
Code:
CStringW csw; //force wide cstring regardless of UNICODE setting
CStringA csa; //force multibyte cstring regardless of UNICODE setting
char foo[]="Test multibyte";
wchar_t bar[]=L"Test wide";
csw=foo;
csa=foo;
csw=bar;
csa=bar;
Re: How can convert a char array to CString?
Quote:
Originally Posted by
hoxsiew
Just use CString's = operator. It will handle the conversion:
Code:
CStringW csw; //force wide cstring regardless of UNICODE setting
CStringA csa; //force multibyte cstring regardless of UNICODE setting
char foo[]="Test multibyte";
wchar_t bar[]=L"Test wide";
csw=foo;
csa=foo;
csw=bar;
csa=bar;
Using CStringW (wide characters) and CStringA (normal char) is NOT a recommendable approach cause dependent on the project settings regarding the used character set you only can use the one or the other when calling MFC functions.
If the CStringW::operator= really could handle single bytes, the constructor CStringW::CStringW(cont char *) would be able as well. So, if the information of hoxsiew is true, you need to check the Read cause you probably would get the text already as wide characters. Check the charr in the debugger. If it doesn't show a printable string you need to use a wchar_t buffer instead of a char buffer.
Re: How can convert a char array to CString?
I suspect that the file contains UNICODE text.
Code:
// Assuming drwFile contains "ABC" or { 'A', 'B', 'C', 0 };
char charr[1000];
drwFile.Read(charr,500);
// str will be initialized correctly regardless of build type because
// CString has constructors for both const char * and const whcar_t *
// and charr is a proper buffer of char types.
CString str(charr);
// Will display "ABC" regardless of build
AfxMessageBox(str);
Code:
// Assuming drwFile contains UNICODE L"ABC" or { 'A', 0, 'B', 0, 'C', 0, 0, 0 };
char charr[1000];
drwFile.Read(charr,500);
// str will NOT BE INITIALIZED correctly regardless of build type because
// charr contains { 'A', 0, 'B', 0, 'C', 0, 0, 0 } which looks like { 'A', '\0' }
// when interpreted as char.
CString str(charr);
// Will display "A" regardless of build
AfxMessageBox(str);
The fix (sloppy though it may be) is to change they type of charr from char to wchar_t.
Re: How can convert a char array to CString?
Quote:
Originally Posted by
itsmeandnobodyelse
Using CStringW (wide characters) and CStringA (normal char) is NOT a recommendable approach cause dependent on the project settings regarding the used character set you only can use the one or the other when calling MFC functions.
I only did that for demonstration purposes, hence the comments.