473,372 Members | 1,530 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,372 software developers and data experts.

string to unsigned char using reinterpret_cast

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
7 7839
weaknessforcats
9,208 Expert Mod 8TB
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
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
9,208 Expert Mod 8TB
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
9,065 Expert Mod 8TB
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
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
9,065 Expert Mod 8TB
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
9,208 Expert Mod 8TB
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

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

Similar topics

2
by: Jason Heyes | last post by:
Is this the right idea? char *ptr; unsigned char *uptr = reinterpret_cast<unsigned char *>(ptr); What about when I start with a char only? char ch; unsigned char *uptr =...
12
by: babak | last post by:
Hi everyone I want to format a string (using sprintf) and put it in a messagebox but however I try to do it, it doesn't seem to work. Here is an example sample of what i try to do: char msg;...
10
by: ruffiano | last post by:
Does the string class have a method for retrieving the address of a string? Thanks.
26
by: =?gb2312?B?wNbA1rTzzOzKpg==?= | last post by:
i wrote: ----------------------------------------------------------------------- ---------------------------------------- unsigned char * p = reinterpret_cast<unsigned char *>("abcdg");...
7
by: Zhang Liming | last post by:
unsigned char rcd; rcd contains 10 chars, such as '1', '2'.... etc. string str; the problem is how to pass the contents in rcd to str with minimal cost?
14
by: moumita | last post by:
Hi All, I need to convert 4 bytes to an unsigned long. Suppose I have one array like unsigned char buf.I need to convert these 4 bytes into a single unsigned long. Is the following piece of code...
1
by: krishna81m | last post by:
I am a newbie and have been trying to understand conversion from double to int and then back to int using the following code was posted on the c++ google group. Could someone help me out with...
11
by: john | last post by:
Hi, at first the code doesn't seem to work. Any ideas?: #include <iostream> #include <cstdlib> int main() { using namespace std;
10
by: Alex Vinokur | last post by:
Hi, Is it possible to do C++-casting from const pair<const unsigned char*, size_t>* to const pair<unsigned char*, size_t>* ? Alex Vinokur
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.