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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. How to convert a char array to CString?

How to convert a char array to CString?

Scheduled Pinned Locked Moved C / C++ / MFC
data-structurestutorialquestion
7 Posts 4 Posters 0 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.
  • U Offline
    U Offline
    User 4396598
    wrote on last edited by
    #1

    Hi All, How to convert a char array into CString? I have one array like this char charr[1000]; .... drwFile.Read(charr,656); //reading some characters from the file CString str; how to store the charr array in to str? Regards, Kumar

    C 1 Reply Last reply
    0
    • U User 4396598

      Hi All, How to convert a char array into CString? I have one array like this char charr[1000]; .... drwFile.Read(charr,656); //reading some characters from the file CString str; how to store the charr array in to str? Regards, Kumar

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      The easy answer is: you don't have to convert, just assign it. But it depends on your UNICODE setting. I suggest you read this article[^] to get a better understanding on character encodings. This will help you a lot in the future.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      U 1 Reply Last reply
      0
      • C Cedric Moonen

        The easy answer is: you don't have to convert, just assign it. But it depends on your UNICODE setting. I suggest you read this article[^] to get a better understanding on character encodings. This will help you a lot in the future.

        Cédric Moonen Software developer
        Charting control [v3.0] OpenGL game tutorial in C++

        U Offline
        U Offline
        User 4396598
        wrote on last edited by
        #3

        Thanks Cedric! The given article is very help full Can't we convert the char array like this? Cstring str(charr); //charr is char array Regards, Kumar

        C C C 3 Replies Last reply
        0
        • U User 4396598

          Thanks Cedric! The given article is very help full Can't we convert the char array like this? Cstring str(charr); //charr is char array Regards, Kumar

          C Offline
          C Offline
          CPallini
          wrote on last edited by
          #4

          Yes, the CString constructor is overloaded to accept that. :)

          If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
          This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
          [My articles]

          1 Reply Last reply
          0
          • U User 4396598

            Thanks Cedric! The given article is very help full Can't we convert the char array like this? Cstring str(charr); //charr is char array Regards, Kumar

            C Offline
            C Offline
            Cedric Moonen
            wrote on last edited by
            #5

            As I said in my previous reply, it all depends on your UNICODE setting. BTW, why don't you just check ? It's one line of code and you can easily verify if it compiles or not. If UNICODE is turned on, the code won't compile.

            Cédric Moonen Software developer
            Charting control [v3.0] OpenGL game tutorial in C++

            C 1 Reply Last reply
            0
            • C Cedric Moonen

              As I said in my previous reply, it all depends on your UNICODE setting. BTW, why don't you just check ? It's one line of code and you can easily verify if it compiles or not. If UNICODE is turned on, the code won't compile.

              Cédric Moonen Software developer
              Charting control [v3.0] OpenGL game tutorial in C++

              C Offline
              C Offline
              CPallini
              wrote on last edited by
              #6

              Nope. It will work. CString constructor automatically handles the conversion, see [^]. :)

              If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler. -- Alfonso the Wise, 13th Century King of Castile.
              This is going on my arrogant assumptions. You may have a superb reason why I'm completely wrong. -- Iain Clarke
              [My articles]

              1 Reply Last reply
              0
              • U User 4396598

                Thanks Cedric! The given article is very help full Can't we convert the char array like this? Cstring str(charr); //charr is char array Regards, Kumar

                C Offline
                C Offline
                Code o mat
                wrote on last edited by
                #7

                Just be carefull, your char array has to be null-terminated to be used to initialize a CString like that or even if you use the = operator ( CString str; str = charr; ). Othwerwise it will keep on reading characters till it finds a null character somewhere after your array and which either results in access violation OR you get garbage in your str.

                > The problem with computers is that they do what you tell them to do and not what you want them to do. < > Sometimes you just have to hate coding to do it well. <

                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