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. string to _bstr_t conversion

string to _bstr_t conversion

Scheduled Pinned Locked Moved C / C++ / MFC
c++help
4 Posts 2 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

    J 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

      J Offline
      J Offline
      jhwurmbach
      wrote on last edited by
      #2

      A string containig a \0 in the middle is not a string in any c-derived language. You are probably better off in not trying to pretend that it is one. Make your string a vector of bytes, and it can happily contain as many \0 as you need. What are you trying to achieve? Maybe stepping back and taking the time to describe your problem in words will help you to find a new approach to its solution. Sandeep Kulkarni wrote: str[11] = 0; _bstr_t bstr = str.c_str(); But only first 11 chars are being copied(till NULL char). Any other behaviour would be a clear violation of guarantees given by the std::string. \0 is a string terminator.


      "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

      S 1 Reply Last reply
      0
      • J jhwurmbach

        A string containig a \0 in the middle is not a string in any c-derived language. You are probably better off in not trying to pretend that it is one. Make your string a vector of bytes, and it can happily contain as many \0 as you need. What are you trying to achieve? Maybe stepping back and taking the time to describe your problem in words will help you to find a new approach to its solution. Sandeep Kulkarni wrote: str[11] = 0; _bstr_t bstr = str.c_str(); But only first 11 chars are being copied(till NULL char). Any other behaviour would be a clear violation of guarantees given by the std::string. \0 is a string terminator.


        "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

        S Offline
        S Offline
        Sandeep Kulkarni
        wrote on last edited by
        #3

        Actually the scenario is a bit different. I have a string, which needs to be encrypted first, then be supplied to another component. While encrypting a "valid" string, there is every possibility(perhaps there is!) that some of the chars be substituted by NULL char. THIS IS VERY MUCH ALLOWED HERE!!! Now such an encrypted string needs to be passed to a method of a COM component which accepts nothing other than _bstr_t !!! Hence the problem!! Thanx anyway,More help will be appreciated.

        J 1 Reply Last reply
        0
        • S Sandeep Kulkarni

          Actually the scenario is a bit different. I have a string, which needs to be encrypted first, then be supplied to another component. While encrypting a "valid" string, there is every possibility(perhaps there is!) that some of the chars be substituted by NULL char. THIS IS VERY MUCH ALLOWED HERE!!! Now such an encrypted string needs to be passed to a method of a COM component which accepts nothing other than _bstr_t !!! Hence the problem!! Thanx anyway,More help will be appreciated.

          J Offline
          J Offline
          jhwurmbach
          wrote on last edited by
          #4

          So you can't change the code of the COMponent? And the way of encryption you need to use is also fixed? Too bad. bstr_t is holding strings in UNICODE encoding, and constructors and operators that take char* are converting the strings to Unicode. Unicode strings sould be resistant to a single \0, as they are only terminated by \0\0. Maybe you can assemble the bstr_t from your string by adding the part until the first \0, then subsequently +=ing the \0, the part until the next \0, the next \0 and so on? Try it out. Sorry that I am of no help here.


          "We trained hard, but it seemed that every time we were beginning to form up into teams we would be reorganised. I was to learn later in life that we tend to meet any new situation by reorganising: and a wonderful method it can be for creating the illusion of progress, while producing confusion, inefficiency and demoralisation." -- Caius Petronius, Roman Consul, 66 A.D.

          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