CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 7 of 7
  1. #1
    Join Date
    Jan 2008
    Location
    India
    Posts
    780

    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.
    IN A DAY, WHEN YOU DON'T COME ACROSS ANY PROBLEMS - YOU CAN BE SURE THAT YOU ARE TRAVELLING IN A WRONG PATH

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,430

    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?
    Victor Nijegorodov

  3. #3
    Join Date
    Oct 2009
    Posts
    577

    Smile 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

  4. #4
    Join Date
    Feb 2005
    Posts
    2,160

    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;

  5. #5
    Join Date
    Oct 2009
    Posts
    577

    Smile Re: How can convert a char array to CString?

    Quote Originally Posted by hoxsiew View Post
    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:perator= 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.

  6. #6
    Join Date
    May 2002
    Posts
    1,435

    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.

  7. #7
    Join Date
    Feb 2005
    Posts
    2,160

    Re: How can convert a char array to CString?

    Quote Originally Posted by itsmeandnobodyelse View Post
    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured