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. String Conversion

String Conversion

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestiondatabasedata-structurestutorial
4 Posts 3 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.
  • M Offline
    M Offline
    Mike Certini
    wrote on last edited by
    #1

    I am currently trying to convert a string that resides in a _bstr_t variable vFieldDate into a char array. I have found that I can type cast this via the (char *) with a variable I named holder which is a pointer to a char. The issue I am having a hard time solving is how to convert this to a char that can be stored within the char Date[ARR]. When I attempt the following code I get the compilation error "Cannot Convert From Char * to Char. How do I extract the value that resides in the holder location and save it into the array? Date[cyc] = holder; The below portions of code are a part of an ADO program that is accessing data from a database. char Date[ARR] = {}; _bstr_t vFieldDate; vFieldDate = rec->Fields->GetItem("Date")->Value; char *holder = (char*)vFieldDate;

    M S 2 Replies Last reply
    0
    • M Mike Certini

      I am currently trying to convert a string that resides in a _bstr_t variable vFieldDate into a char array. I have found that I can type cast this via the (char *) with a variable I named holder which is a pointer to a char. The issue I am having a hard time solving is how to convert this to a char that can be stored within the char Date[ARR]. When I attempt the following code I get the compilation error "Cannot Convert From Char * to Char. How do I extract the value that resides in the holder location and save it into the array? Date[cyc] = holder; The below portions of code are a part of an ADO program that is accessing data from a database. char Date[ARR] = {}; _bstr_t vFieldDate; vFieldDate = rec->Fields->GetItem("Date")->Value; char *holder = (char*)vFieldDate;

      M Offline
      M Offline
      Mike Certini
      wrote on last edited by
      #2

      This question has been anwered with: #include <cstring> //... strcpy( Date, holder );

      A 1 Reply Last reply
      0
      • M Mike Certini

        This question has been anwered with: #include <cstring> //... strcpy( Date, holder );

        A Offline
        A Offline
        Andrew Brock
        wrote on last edited by
        #3

        This will only work if he is using a wide string, in which case it would be wcscpy because BSTR is a wide string. To copy a BSTR into an ASCII string, you will need to convert the character set.

        1 Reply Last reply
        0
        • M Mike Certini

          I am currently trying to convert a string that resides in a _bstr_t variable vFieldDate into a char array. I have found that I can type cast this via the (char *) with a variable I named holder which is a pointer to a char. The issue I am having a hard time solving is how to convert this to a char that can be stored within the char Date[ARR]. When I attempt the following code I get the compilation error "Cannot Convert From Char * to Char. How do I extract the value that resides in the holder location and save it into the array? Date[cyc] = holder; The below portions of code are a part of an ADO program that is accessing data from a database. char Date[ARR] = {}; _bstr_t vFieldDate; vFieldDate = rec->Fields->GetItem("Date")->Value; char *holder = (char*)vFieldDate;

          S Offline
          S Offline
          Stefan_Lang
          wrote on last edited by
          #4

          Disregarding the method of conversion, the assignment you used attempts to assigns an array of characters to single character: Date is defined as an array of ARR characters, and Date[cyc] denotes the character with the index cyc within that array. What was your intention? I suspect that what you really wanted is to define Date as an array of strings rather than as an array of characters. In this case you should define Date as char* Date[ARR];. On a sidenote, what you are trying to achieve is like pouring milk into a crate for beer bottles: you can't just go ahead and assign the contents of one variable to another variable that is designed to hold something completely different - doing this will usually result in spilling the contents. Instead you have to use the proper function or method for transfering the contents of one variable into the other. Plus you might need to allocate proper containers first: so first thing, pick up an empty beer bottle, then pour the milk into it, and only then you can put that bottle into the crate. With respect to these strings, the Date array corresponds to the crate for beer bottles. The beer bottles correspond to char-strings, and the milk corresponds to the contents of that _bstr_t. So what you have to do is: - allocate an array of char, big enough to hold the _bstr_t - call the appropriate method to convert a _bstr_t into a char*, and then - put it into the Date array.

          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