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. Need to convert CString to std::string

Need to convert CString to std::string

Scheduled Pinned Locked Moved C / C++ / MFC
help
10 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.
  • K Offline
    K Offline
    krishnakumartm
    wrote on last edited by
    #1

    Hi i have a problem of concatenation two CString variables and assign to std::string here i need to convert CString to std::string and have to concatenate if any possible help me Thanks in advance

    ---------------------------- KRISHNA KUMAR T M

    C CPalliniC 2 Replies Last reply
    0
    • K krishnakumartm

      Hi i have a problem of concatenation two CString variables and assign to std::string here i need to convert CString to std::string and have to concatenate if any possible help me Thanks in advance

      ---------------------------- KRISHNA KUMAR T M

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

      The problem with your approach is when you have support for UNICODE. CString support both non-unicode and unicode while std::string support only non-unicode strings. Take a look at this post[^] to see how support both unicode and non-unicode strings. I also suggest you have a look at this excellent article[^] to get a better grip on all the different strings and 'conversions'.

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

      1 Reply Last reply
      0
      • K krishnakumartm

        Hi i have a problem of concatenation two CString variables and assign to std::string here i need to convert CString to std::string and have to concatenate if any possible help me Thanks in advance

        ---------------------------- KRISHNA KUMAR T M

        CPalliniC Offline
        CPalliniC Offline
        CPallini
        wrote on last edited by
        #3

        And what's the problem? Are you doing a UNICODE build? :)

        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]

        In testa che avete, signor di Ceprano?

        K 1 Reply Last reply
        0
        • CPalliniC CPallini

          And what's the problem? Are you doing a UNICODE build? :)

          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]

          K Offline
          K Offline
          krishnakumartm
          wrote on last edited by
          #4

          Yes i am building in UNICODE mode

          ---------------------------- KRISHNA KUMAR T M

          CPalliniC 1 Reply Last reply
          0
          • K krishnakumartm

            Yes i am building in UNICODE mode

            ---------------------------- KRISHNA KUMAR T M

            CPalliniC Offline
            CPalliniC Offline
            CPallini
            wrote on last edited by
            #5

            Then

            CString sFirstName=_T("Foo"), sLastName=_T("Goo");
            CT2A sANSIFullName(sFirstName + sLastName);
            std::string sResult(sANSIFullName);

            :)

            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]

            In testa che avete, signor di Ceprano?

            T 1 Reply Last reply
            0
            • CPalliniC CPallini

              Then

              CString sFirstName=_T("Foo"), sLastName=_T("Goo");
              CT2A sANSIFullName(sFirstName + sLastName);
              std::string sResult(sANSIFullName);

              :)

              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]

              T Offline
              T Offline
              toxcct
              wrote on last edited by
              #6

              no ! use std::wstring instead !

              [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

              CPalliniC 1 Reply Last reply
              0
              • T toxcct

                no ! use std::wstring instead !

                [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                CPalliniC Offline
                CPalliniC Offline
                CPallini
                wrote on last edited by
                #7

                No need to be integralist, my friend :rolleyes: i.e. I usually don't make philosophical assumptions on the reason behind the question. If he really needs a std::string (maybe he need to call a function of a library out of his control), then the proposed code works; on the other hand, since he probably needs that, someone else already gave the right links to get more insights on using UNICODE or ANSI strings in his own project. :)

                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]

                In testa che avete, signor di Ceprano?

                T 1 Reply Last reply
                0
                • CPalliniC CPallini

                  No need to be integralist, my friend :rolleyes: i.e. I usually don't make philosophical assumptions on the reason behind the question. If he really needs a std::string (maybe he need to call a function of a library out of his control), then the proposed code works; on the other hand, since he probably needs that, someone else already gave the right links to get more insights on using UNICODE or ANSI strings in his own project. :)

                  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]

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #8

                  CPallini wrote:

                  No need to be integralist, my friend

                  but we are not sheeps, we are thinking human being too, and answering the board also means advising well... Using CT2A() or CW2A() will potentially loose data if the OP is using "true" wide-characters (those which really need more than 1 byte to be). Using std::wstring will not though. BTW, your point about the external lib is true ;)

                  [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                  CPalliniC 1 Reply Last reply
                  0
                  • T toxcct

                    CPallini wrote:

                    No need to be integralist, my friend

                    but we are not sheeps, we are thinking human being too, and answering the board also means advising well... Using CT2A() or CW2A() will potentially loose data if the OP is using "true" wide-characters (those which really need more than 1 byte to be). Using std::wstring will not though. BTW, your point about the external lib is true ;)

                    [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                    CPalliniC Offline
                    CPalliniC Offline
                    CPallini
                    wrote on last edited by
                    #9

                    Saying this

                    toxcct wrote:

                    no ! use std::wstring instead !

                    is integralism. Saying this

                    toxcct wrote:

                    but we are not sheeps, we are thinking human being too, and answering the board also means advising well... Using CT2A() or CW2A() will potentially loose data if the OP is using "true" wide-characters (those which really need more than 1 byte to be). Using std::wstring will not though.

                    is of thinking human. Saying this

                    toxcct wrote:

                    BTW, your point [...] is true

                    is great! ;P :-D

                    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]

                    In testa che avete, signor di Ceprano?

                    T 1 Reply Last reply
                    0
                    • CPalliniC CPallini

                      Saying this

                      toxcct wrote:

                      no ! use std::wstring instead !

                      is integralism. Saying this

                      toxcct wrote:

                      but we are not sheeps, we are thinking human being too, and answering the board also means advising well... Using CT2A() or CW2A() will potentially loose data if the OP is using "true" wide-characters (those which really need more than 1 byte to be). Using std::wstring will not though.

                      is of thinking human. Saying this

                      toxcct wrote:

                      BTW, your point [...] is true

                      is great! ;P :-D

                      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]

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #10

                      ROTFL ! :laugh:

                      [VisualCalc][Binary Guide][CommDialogs] | [Forums Guidelines]

                      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