Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to Convert CString to char array?

How to Convert CString to char array?

Scheduled Pinned Locked Moved C / C++ / MFC
questiondata-structuresperformancetutorial
5 Posts 5 Posters 4 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • T Offline
    T Offline
    TechAvtar
    wrote on last edited by
    #1

    I Have a CString which contains port names. CString cs = "COM1"; char portname[5]; 1. What is the memory representation of cs above (I have this question since I think it is multibyte/wide char)? 2. How to copy above cs into portname ? I need portname to contain {'C','O','M','1','\0'} Thanks JC

    Y M I A 4 Replies Last reply
    0
    • T TechAvtar

      I Have a CString which contains port names. CString cs = "COM1"; char portname[5]; 1. What is the memory representation of cs above (I have this question since I think it is multibyte/wide char)? 2. How to copy above cs into portname ? I need portname to contain {'C','O','M','1','\0'} Thanks JC

      Y Offline
      Y Offline
      Yusuf
      wrote on last edited by
      #2

      Look CString Management[^].

      Yusuf May I help you?

      1 Reply Last reply
      0
      • T TechAvtar

        I Have a CString which contains port names. CString cs = "COM1"; char portname[5]; 1. What is the memory representation of cs above (I have this question since I think it is multibyte/wide char)? 2. How to copy above cs into portname ? I need portname to contain {'C','O','M','1','\0'} Thanks JC

        M Offline
        M Offline
        MS_TJ
        wrote on last edited by
        #3

        TCHAR path[ MAX_PATH ]; Cstring str; _tcscpy(path, str);

        1 Reply Last reply
        0
        • T TechAvtar

          I Have a CString which contains port names. CString cs = "COM1"; char portname[5]; 1. What is the memory representation of cs above (I have this question since I think it is multibyte/wide char)? 2. How to copy above cs into portname ? I need portname to contain {'C','O','M','1','\0'} Thanks JC

          I Offline
          I Offline
          Iain Clarke Warrior Programmer
          wrote on last edited by
          #4

          1/ The memory representation will change depending on whether the UNICODE define is there or not. The default changes depending on which version of Visual studio you have. VS6, it's char. I think all later ones, it's w_char, or 16bits. 2/ There's a conversion to LPCTSTR operator in the CString class, so you can pass it to const TCHAR * parameters of other functions. So, your question is trivial:

          lstrcpy (szBufferOut, sMyString);

          Have a look at the following article for more help: The Complete Guide to C++ Strings, Part I - Win32 Character Encodings[^] Good luck, Iain.

          I have now moved to Sweden for love (awwww).

          1 Reply Last reply
          0
          • T TechAvtar

            I Have a CString which contains port names. CString cs = "COM1"; char portname[5]; 1. What is the memory representation of cs above (I have this question since I think it is multibyte/wide char)? 2. How to copy above cs into portname ? I need portname to contain {'C','O','M','1','\0'} Thanks JC

            A Offline
            A Offline
            Aescleal
            wrote on last edited by
            #5

            Hi, You've got two problems here: - Converting from potentially wide to narrow charcaters - Getting rid of any const The first means that you can't use something like _tcscpy and TCHARs as you want real, honest, chars, not chars or wchar_ts depending on some preprocessor macro. The second means you've got to do a real copy and you can't just use an implicit conversion. To solve the second problem you've got to do a copy. To solve the first you either copy the string using wcstombs or strcpy depending on whether the CString is encoded as unicode as not. You can either use the preprocessor to select which function you use, but that's really sucky like most uses of the preprocessor in C++ (avoid it like the plague) or you can use function overloading:

            void to_narrow( const CStringA &to_convert, char *out )
            {
            ...
            }

            void to_narrow( const CStringW &to_convert, char *out )
            {
            ...
            }

            I haven't bothered including the code as there's another way you can do the conversion which relies more on the C++ standard library and has less pointers flopping about and confusing the issue. In this version I'm returning a std::vector as it sorts out any memory management for the caller:

            std::vector<char> to_narrow( const CStringA &to_convert )
            {
            const char *convert_start = to_convert;
            const char *convert_end = convert_start + to_convert.GetLength() + 1;

            return std::vector<char>( convert\_start, convert\_end );
            

            }

            std::vector<char> to_narrow( const CStringW &to_convert )
            {
            const wchar_t *convert_start = to_convert;

            std::vector<char> output( to\_convert.GetLength() \* 4 + 1 );
            output.resize( wcstombs( &output\[ 0 \], to\_convert, output.size() ) );
            return output;
            

            }

            The supperfluous +1s everywhere are due to CString::GetLength not including the terminating '\0' character in the length. The *4 is just in case Microsoft ever support utf-8 as a character set - it gives the oodles of space for really weird characters Any questions just shout. Cheers, Ash PS: This code was written from memory and isn't tested, there might be some problems but the idea's sound and in use all over the place.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups