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. ATL / WTL / STL
  4. STL string to _bstr_t conversion

STL string to _bstr_t conversion

Scheduled Pinned Locked Moved ATL / WTL / STL
c++help
7 Posts 5 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.
  • S Offline
    S Offline
    Sandeep Kulkarni
    wrote on last edited by
    #1

    hi i have an STL string object, which has a NULL char in the MIDDLE. For ex: #include using namespace std; .... string str = "abcdefghijklmnopqrstuvwxyz"; str[11] = 0; // str.size() still equals 26 Now i want to convert this string to _bstr_t object, retaining the NULL char and the chars after that. I tried this: _bstr_t bstr = str.c_str(); But only first 11 chars are being copied(till NULL char). Pls help TIA

    I M P 3 Replies Last reply
    0
    • S Sandeep Kulkarni

      hi i have an STL string object, which has a NULL char in the MIDDLE. For ex: #include using namespace std; .... string str = "abcdefghijklmnopqrstuvwxyz"; str[11] = 0; // str.size() still equals 26 Now i want to convert this string to _bstr_t object, retaining the NULL char and the chars after that. I tried this: _bstr_t bstr = str.c_str(); But only first 11 chars are being copied(till NULL char). Pls help TIA

      I Offline
      I Offline
      ian mariano
      wrote on last edited by
      #2

      Just replace all '\0' characters in your std::string with some token character which you can convert back once it's a BSTR. Remember BSTRings are 2 bytes wide in Unicode, so NULL is 0x0000.

      Ian Mariano - http://www.ian-space.com/
      "We are all wave equations in the information matrix of the universe" - me

      1 Reply Last reply
      0
      • S Sandeep Kulkarni

        hi i have an STL string object, which has a NULL char in the MIDDLE. For ex: #include using namespace std; .... string str = "abcdefghijklmnopqrstuvwxyz"; str[11] = 0; // str.size() still equals 26 Now i want to convert this string to _bstr_t object, retaining the NULL char and the chars after that. I tried this: _bstr_t bstr = str.c_str(); But only first 11 chars are being copied(till NULL char). Pls help TIA

        M Offline
        M Offline
        Michael Dunn
        wrote on last edited by
        #3

        basic_string, by definition, does not allow embedded 0 characters. When you stick a 0 in yourself, you're breaking the semantics of basic_string, so anything after that is not guaranteed to work. You'll need to copy the string manually. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- If my rhyme was a drug, I'd sell it by the gram.

        N 1 Reply Last reply
        0
        • M Michael Dunn

          basic_string, by definition, does not allow embedded 0 characters. When you stick a 0 in yourself, you're breaking the semantics of basic_string, so anything after that is not guaranteed to work. You'll need to copy the string manually. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- If my rhyme was a drug, I'd sell it by the gram.

          N Offline
          N Offline
          Nemanja Trifunovic
          wrote on last edited by
          #4

          Michael Dunn wrote: basic_string, by definition, does not allow embedded 0 characters. Where did you find this information? AFAIK, the C++ standard does not mention this, and the only function that would be affected by embedded 0 characters is c_str()

          M 1 Reply Last reply
          0
          • N Nemanja Trifunovic

            Michael Dunn wrote: basic_string, by definition, does not allow embedded 0 characters. Where did you find this information? AFAIK, the C++ standard does not mention this, and the only function that would be affected by embedded 0 characters is c_str()

            M Offline
            M Offline
            Michael Dunn
            wrote on last edited by
            #5

            hmm... That was just one of those things I saw in passing and it happened to stick in my memory. Of course, I can't find it now. :( So I could be wrong, don't quote me (well, you already did... don't quote me elsewhere ;) ) The closest I can find is this:

            basic_string::c_str const E *c_str() const; The member function returns a pointer to a nonmodifiable C string constructed by adding a terminating null element (E(0)) to the controlled sequence. Calling any non-const member function for *this can invalidate the pointer.

            That's one aspect that treats E(0) as special, E(0) being '\0' for character strings. Since the caller of c_str() can look for E(0) to mark the end of the returned string, an E(0) in the middle of the string also looks like the end. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Kosh reminded me of some of the prima-donna programmers I've worked with. Knew everything but when you asked them a question; never gave you a straight answer.   -- Michael P. Butler in the Lounge

            N 1 Reply Last reply
            0
            • M Michael Dunn

              hmm... That was just one of those things I saw in passing and it happened to stick in my memory. Of course, I can't find it now. :( So I could be wrong, don't quote me (well, you already did... don't quote me elsewhere ;) ) The closest I can find is this:

              basic_string::c_str const E *c_str() const; The member function returns a pointer to a nonmodifiable C string constructed by adding a terminating null element (E(0)) to the controlled sequence. Calling any non-const member function for *this can invalidate the pointer.

              That's one aspect that treats E(0) as special, E(0) being '\0' for character strings. Since the caller of c_str() can look for E(0) to mark the end of the returned string, an E(0) in the middle of the string also looks like the end. --Mike-- Personal stuff:: Ericahist | Homepage Shareware stuff:: 1ClickPicGrabber | RightClick-Encrypt CP stuff:: CP SearchBar v2.0.2 | C++ Forum FAQ ---- Kosh reminded me of some of the prima-donna programmers I've worked with. Knew everything but when you asked them a question; never gave you a straight answer.   -- Michael P. Butler in the Lounge

              N Offline
              N Offline
              Nemanja Trifunovic
              wrote on last edited by
              #6

              Yep, embedded zeros can screw c_str. However, I made this little experiment:

              char buffer\[\] = {'h','e','l','l','o', 0, 'w', 'o', 'r', 'l', 'd'};
              string s (buffer, sizeof(buffer));
              cout << s << " length = " << s.length()<< endl;
              

              And the output (VC 7.1) is:

              hello world length = 11

              1 Reply Last reply
              0
              • S Sandeep Kulkarni

                hi i have an STL string object, which has a NULL char in the MIDDLE. For ex: #include using namespace std; .... string str = "abcdefghijklmnopqrstuvwxyz"; str[11] = 0; // str.size() still equals 26 Now i want to convert this string to _bstr_t object, retaining the NULL char and the chars after that. I tried this: _bstr_t bstr = str.c_str(); But only first 11 chars are being copied(till NULL char). Pls help TIA

                P Offline
                P Offline
                Paul Ranson
                wrote on last edited by
                #7

                The tidiest way would be to use ATL's CComBSTR rather than _bstr_t. using namespace std; .... string str = "abcdefghijklmnopqrstuvwxyz"; str[11] = 0; // str.size() still equals 26 CComBSTR bstr ( str.data (), str.size ()) ; Paul

                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