String Conversion
-
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;
-
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;
This question has been anwered with: #include <cstring> //... strcpy( Date, holder );
-
This question has been anwered with: #include <cstring> //... strcpy( Date, holder );
This will only work if he is using a wide string, in which case it would be
wcscpy
becauseBSTR
is a wide string. To copy aBSTR
into an ASCII string, you will need to convert the character set. -
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;
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.