By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,948 Members | 663 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,948 IT Pros & Developers. It's quick & easy.

string to unsigned char using reinterpret_cast

P: 3
I am attempting to convert a string to unsigned char. The Method I use appears to work but the function call fails.

I am using MSVC++ Express 2.0.50727 on Win XP with an AMD Processor. Forgive me if the following is verbose, however, I am ctumped as to what little element is hindering me so i'll try to be as clear as I can.

The aim of my code is to call an SQL database (MS SQL Server 2005) using ODBC and all works well up until now.

Expand|Select|Wrap|Line Numbers
  1. //inside a sqlext.h file there is a typedef of UCHAR
  2. typedef UCHAR   unsigned char
  3.  
  4. //in my main() function
  5. UCHAR SQLstr[128] = "execute stored_procedure_FOOBAR 3, 'spam' "
  6. retcode = SQLPrepare (hStmt, SQLstr, sizeof SQLstr);
  7. if(retcode == something_bad){
  8.      cout << "sqlprepare error \n";
  9.      return 9;
  10. }
  11. retcode = SQLNumResultCols(hStmt, &cols);
  12. if(retcode == something_bad){
  13.      cout<<"SQLNUmResultCols error \n";
  14.      return 9
  15. }
  16.  
(Here hStmt is a handle to the sql statement) However, I now need to concatenate the different sql statement variables together and turn them into UCHAR. To demonstrate this, suppose I have made a string of my SQL statement: stringySQL, exactly the same as the above UCHAR (which works btw) but now a string...

Expand|Select|Wrap|Line Numbers
  1. string stringySQL =  "execute stored_procedure_FOOBAR 3, 'spam' ";
  2. char temp[128] ;
  3. strcpy(temp, stringySQL.c_str());
  4. UCHAR SQLstr[128];
  5. strcpy(reinterpret_cast<char*>(SQLstr), temp);
  6.  
This code complies and runs but gives the error "SQLNUmResultCols error". I even tried the following:
Expand|Select|Wrap|Line Numbers
  1. UCHAR SQLtemp[128] ="execute stored_procedure_FOOBAR 3, 'spam' ";
  2. //if I now use this it works...but
  3. UCHAR SQLstr[128];
  4. for(i=0;i<128;i++){
  5.      SQLstr[i] = SQLtemp[i] 
  6. }
  7. //if I now use SQLstr it doesn't!
  8.  
If anyone can offer advice it would be greatly appreciated. Rich.
Aug 14 '07 #1
Share this Question
Share on Google+
7 Replies


weaknessforcats
Expert Mod 5K+
P: 9,197
One question is why the UCHAR? C-strings are CHAR.

Actually, since this is Windows, it should be TCHAR to support ASCSII/UNICODE mappings.

Expand|Select|Wrap|Line Numbers
  1. string stringySQL =  "execute stored_procedure_FOOBAR 3, 'spam' ";
  2. char temp[128] ;
  3. strcpy(temp, stringySQL.c_str());
  4. TCHAR SQLstr[128];
  5. strcpy(SQLstr, temp);
  6. cout << SQLstr << endl;
  7.  
Aug 14 '07 #2

P: 3
One question is why the UCHAR? C-strings are CHAR.

Actually, since this is Windows, it should be TCHAR to support ASCSII/UNICODE mappings.
The function SQLPrepare takes UCHAR* as its input argument and did not work when given TCHAR. (even as a whole string, even using reinterpret_cast(UCHAR*)

On the bright side: I have just solved the problem to some, practical, extent. However I am intrigued as to why my solution works, If you want to tell me I would be happy to hear why it works.

Expand|Select|Wrap|Line Numbers
  1.       string stringySQL =  "execute stored_procedure_FOOBAR 3, 'spam'";
  2.       char temp[128] ;
  3.       strcpy(temp, stringySQL.c_str());
  4.       UCHAR SQLstr[128] = "______any_length <= length(stringySQL)______";
  5.       strcpy(reinterpret_cast<char*>(SQLstr), temp);
  6.  
Again, many thanks,
Rich
Aug 15 '07 #3

weaknessforcats
Expert Mod 5K+
P: 9,197
Is this your SQLPrepare()??
SQLRETURN SQLPrepare(
SQLHSTMT StatementHandle,
SQLCHAR * StatementText,
SQLINTEGER TextLength);
Maybe you could post the function prototype of your SQLPrepare becuse this one has no UCHAR arguiments.
Aug 15 '07 #4

Banfa
Expert Mod 5K+
P: 8,916
Is this your SQLPrepare()??


Maybe you could post the function prototype of your SQLPrepare becuse this one has no UCHAR arguiments.
SQLCHAR is a unsigned type (which a suspect UCHAR is too from it's name)

Excert from SQLTYPES.H
Expand|Select|Wrap|Line Numbers
  1. typedef unsigned char   SQLCHAR;
  2.  
However richardpeter the point I think weaknessforcats is trying to make is a valid one which is if you are going to call a function, since you are taking the trouble to change the datatype it is probably worth taking to trouble to change the datatype to the one the function is expecting.
Aug 15 '07 #5

P: 3
SQLCHAR is a unsigned type (which a suspect UCHAR is too from it's name)

Excert from SQLTYPES.H
Expand|Select|Wrap|Line Numbers
  1. typedef unsigned char   SQLCHAR;
  2.  
However richardpeter the point I think weaknessforcats is trying to make is a valid one which is if you are going to call a function, since you are taking the trouble to change the datatype it is probably worth taking to trouble to change the datatype to the one the function is expecting.
My apologies for the confusion, you are both correct: SQLCHAR is UCHAR which is unsigned char. like Banfa mentions, they both have typedefs located in the headder file sqext.h.

What weaknessforcats said is correct too, however in my program I have to concatenate strings to make my SQL statement and then convert these strings to SQLCHAR or equivalently UCHAR or equivalently unsigned char (they all work).

The code I posted above to do this does work, but I am now more intrigued as to why I have to initialise my UCHAR/SQLCHAR string to something of equal or lesser length than my desired SQL statement?

Many thanks
Rich
Aug 27 '07 #6

Banfa
Expert Mod 5K+
P: 8,916
Sounds strange, I would suggest that you use the debugger to examine the exact contents of the sting you pass to SQLPrepare in both the working and non-working case.
Aug 27 '07 #7

weaknessforcats
Expert Mod 5K+
P: 9,197
Going back to Post #1:
//inside a sqlext.h file there is a typedef of UCHAR
typedef UCHAR unsigned char

//in my main() function
UCHAR SQLstr[128] = "execute stored_procedure_FOOBAR 3, 'spam' "
The typedef is backwards. It defines UCHAR as an unsigned char but does not define an unsigned char as UCHAR.

I changed the typedef below and looked at the array in the debugger and it is properly initialized.

There should be no need to use a string or a strcpy.

Expand|Select|Wrap|Line Numbers
  1. typedef unsigned char UCHAR;
  2.  int main()
  3. {
  4. //in my main() function
  5. UCHAR SQLstr[128] = "execute stored_procedure_FOOBAR 3, 'spam' ";
  6. }
  7.  
However, this code:
retcode = SQLPrepare (hStmt, SQLstr, sizeof SQLstr);
may have a bug in it. sizeof SQLStr is 128 which is the size of the buffer. The SQLPrepare() for ODBC requires the third argument to be the number of characters in the query , in this case 42, and not the size of the buffer.
Aug 27 '07 #8

Post your reply

Sign in to post your reply or Sign up for a free account.