I have a variable with data type string and need to convert it to unsigned char...
Please help
I have a variable with data type string and need to convert it to unsigned char...
Please help
That's nonsensical unless you want a single character from the string. A string is more or less a collection of unsigned char. What are you trying to do?
My best code is written with the delete key.
Code:/*============================= Manipulating strings =============================*/ #include <iostream> #include <string> using namespace std; int main() { string my_string="hello there"; for(int i = 0; i < my_string.length(); i++) { cout<<my_string[i]<<endl; //single out characters here if you want } //or if you want to convert a string to an integer //try using <sstream> cin.get(); }
Code:/*============================= Or if you really wanted to... The long winded way of converting a string to a char array =============================*/ #include <iostream> #include <string> using namespace std; int main() { char array[100]; string my_string="hello there"; for(int i = 0; i < my_string.length(); i++) { array[i]=my_string[i]; } array[my_string.length()]='\0'; //get rid of the rubbish cout<<array; cin.get(); }
Last edited by treenef; 10-27-2005 at 05:22 AM.
I wanted to ask that I have a string of basic type and want to convert it into unicode that is unsigned char....Please help....
Okie let me be more specific... I am using SQLConnect... I am recieving the username and password through a string type variable..I need to pass it to SQLConnect which accepts only unsigned char type....I am really stuck..any help appreciated....
>I need to pass it to SQLConnect which accepts only unsigned char type
Unsigned char or pointer to unsigned char? There's a huge difference if it's a pointer, and that makes more sense. The string class has a member function, c_str, that gives you what you're probably looking for.
My best code is written with the delete key.
No am not talking about a pointer....Please let me know any information i am missing out on SQLConnect function...As far as i know it accepts only unsigned char....I am getting a string cariable...
>No am not talking about a pointer....
The reference that I'm looking at sure seems to think that SQLConnect takes a pointer to SQLCHAR for the user name and password, and SQLCHAR is defined as unsigned char. You can cast (C-style or reinterpret_cast<>) the result of c_str() to SQLCHAR * and be done with it.
My best code is written with the delete key.
Originally Posted by whistle
unicode strings are not unsigned char -- they are implementation dependent. They use wchar_t data type, which is typedefed as unsigned sort in MS-Windows os, and unsigned long in linux (maybe unix too, I don't know). And none of those are the same as SQLCHAR mentioned earlier.
Can somebody please explain to me in simple terms.I got a string variable x, and need to pass it to SQLConnect as username. PLLLLLEAAAASSEEEE HEEELPPPPP
These guys explained it very well...if you don't understand what they are saying you should check out some tutorials on C. Other than that, you should do some work for yourself - http://msdn.microsoft.com/library/de...data_types.asp . SQLCHAR* is indeed a const char* therefore you CAN use string::c_str() and pass it in as your username.
-"What we wish, we readily believe, and what we ourselves think, we imagine others think also."PHP Code:
sadf
I am sorry for the trouable but the SQLConnect is not accepting a const char*. I dunno what is wrong but it asks for unsigned char*.Hence the confusion.
Thanks anyways for all the help...
>I dunno what is wrong but it asks for unsigned char*.Hence the confusion.
You're simply not listening to those of us who have given you a correct answer. For example, I said this not too long ago:
What does that mean? It means you do this:Originally Posted by Prelude
Code:unsigned char *s = reinterpret_cast<SQLCHAR *> ( server.c_str() ); unsigned char *u = reinterpret_cast<SQLCHAR *> ( user.c_str() ); unsigned char *p = reinterpret_cast<SQLCHAR *> ( pass.c_str() ); rc = SQLConnect ( h, s, server.size(), u, user.size(), p, pass.size() );
My best code is written with the delete key.