Thread: Help....String to Unsigned char conversion

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    6

    Help....String to Unsigned char conversion

    I have a variable with data type string and need to convert it to unsigned char...
    Please help

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    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.

  3. #3
    Super Moderater.
    Join Date
    Jan 2005
    Posts
    374
    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.

  4. #4
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    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....

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    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....

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  7. #7
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    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...

  8. #8
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    1,267
    Quote Originally Posted by whistle
    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....

    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.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    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

  11. #11
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    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 

  12. #12
    Registered User
    Join Date
    Oct 2005
    Posts
    6
    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...

  13. #13
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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:
    Quote Originally Posted by Prelude
    You can cast (C-style or reinterpret_cast<>) the result of c_str() to SQLCHAR * and be done with it.
    What does that mean? It means you do this:
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. newbie needs help with code
    By compudude86 in forum C Programming
    Replies: 6
    Last Post: 07-23-2006, 08:54 PM
  3. convert maybe maybe not..
    By pico in forum C Programming
    Replies: 7
    Last Post: 03-15-2005, 10:13 AM
  4. ANY BODY WILLING TO HELP ME WITH Microsoft Visual C++
    By BiG pImPiN fOoL in forum C++ Programming
    Replies: 12
    Last Post: 11-04-2001, 06:03 PM
  5. String -> unsigned char array conversion....
    By SublicK in forum C++ Programming
    Replies: 1
    Last Post: 10-23-2001, 12:18 PM