UCHAR conversion to char []

This is a discussion on UCHAR conversion to char [] within the C++ Programming forums, part of the General Programming Boards category; I think I am missing the concept behind the unassigned char. I understand that UCHAR stands for unassigned char. I ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    164

    UCHAR conversion to char []

    I think I am missing the concept behind the unassigned char. I understand that UCHAR stands for unassigned char. I am not used to this format however. I am using it to connect and run sql commands to MS SQL Server db.

    My problem is, I want a combination of different string variables to make one large string and pass that as my sql statement. I am familiar with combining strings like this:

    Code:
    //using string.h namespace std;
    string char1 ="Hello ";
    string char2= "World";
    char1 += char2;
    //char1 now equals "Hello World"
    //and to pass this value to file or ouput
    strcpy(somestring, char1.c_str());
    However how can I take a variable type of UCHAR and a character of arrays that can be passed to a handle such as:
    Code:
    char  buff[128]  = "Select Routename, LotID, Location FROM sublot WHERE LotID like '05101182025734%'";
    UCHAR                   szSqlStr[128];
    
    //open db code ect.... and then combine variables and execute
    //currently trying to do this without success
    sprintf((char *)szSqlStr,"%p",&buff);
    cout <<szSqlStr<<endl;
    
    /*//it looks correct on screen but I get garbage back, and I know the statement works because when I set the szSqlStr equal to this directly it works */
    
    //part that is returning junk
     // Prepare the SQL statement by assigning it to the statement handle
    
                                    retcode = SQLPrepare (hStmt, szSqlStr, sizeof (szSqlStr));
    
      
    
                                    // Execute the SQL statement handle
    
                                    retcode = SQLExecute (hStmt);
    
                                    
    
                                    // Project only column 1 which is the models
    
                                    SQLBindCol (hStmt, 1, SQL_C_CHAR, szRoutename, sizeof(szRoutename), &cbRoutename);
    								SQLBindCol (hStmt, 2, SQL_C_CHAR, szLotID, sizeof(szLotID), &cbLotID);
    								SQLBindCol (hStmt, 3, SQL_C_CHAR, szLocation, sizeof(szLocation), &cbLocation);
      
    
                                    // Get row of data from the result set defined above in the statement
    
                                    retcode = SQLFetch (hStmt);
    
    								cout <<szRoutename<<endl;
    								cout <<szLotID<<endl;
    								cout <<szLocation<<endl;
    Sorry if that took too long to get out... Thanks for any help, this is driving me nutz!

  2. #2
    Registered User
    Join Date
    May 2004
    Posts
    164
    Sorry I meant to put this in the C++ forum.

  3. #3
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    UCHAR is an unsigned char.
    Also, you use:
    sprintf((char *)szSqlStr,"%p",&buff);

    %p prints out the memory location that a pointer is pointing at. I think you want %s, which would be replaced with the string. (Be sure that szSqlStr is long enough for buff)
    sprintf((char *)szSqlStr, "%s", buff);

    [EDIT] Since you're not changing buff, you could pass buff as the SQL query. Also: http://www.cplusplus.com/ref/cstdio/printf.html
    Last edited by Cactus_Hugger; 09-21-2005 at 06:27 PM.
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  4. #4
    Registered User
    Join Date
    May 2004
    Posts
    164
    sorry I meant to change that back originally I had used the
    Code:
    sprintf ((char*)szSqlStr, "%s", buff);
    however that did not work. I don't think I am over loading the varaible. Still puzzled, I am looking at the link now. Thanks for the reply, any other ideas are greatly appreciated.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,934
    You could try:
    Code:
    strcpy( (char*)szSqlStr, buff );
    But if sprintf() doesn't work, I doubt strcpy() will either.

  6. #6
    Registered User
    Join Date
    May 2004
    Posts
    164
    no luck, on the strcpy, I tried that one ealier without success, again it looked good on the cout<< however the return was garble.

    Wonder if this is because of the null character at the end of the array of char, buffer? Maybe it does not recognize that in my statement when passing it to the db as a sql statement?

  7. #7
    Registered User
    Join Date
    May 2004
    Posts
    164
    That was it, the null character, I just chopped it off by shortning my UCHAR by one less than the char string and it worked! I got my designated results!

    Code:
    char buff [81] = "my statement";
    UCHAR szSqlStr [80]; // this got rid of the null character!
    
    // the results came back good!

  8. #8
    int x = *((int *) NULL); Cactus_Hugger's Avatar
    Join Date
    Jul 2003
    Location
    Banks of the River Styx
    Posts
    902
    Using this code:
    Code:
    char buff [81] = "my statement";
    UCHAR szSqlStr [80];
    
    sprintf((char *) szSqlStr, "%s", buff);
    There is still a null at the end of szSqlStr, as sprintf null terminates your string. And, if the string in buff is 80 bytes long, then 81 bytes is written to szSqlStr! This may or may not crash your program, and in any case, the null is still there.

    You have a cout statement in your original post. What is szSqlStr after using sprintf/strcpy? (Better yet: use C++ strings. I have no experience with C++ strings, but they'd probably be easier and safer to use.) If the string is correct, what do the SQL functions return?
    long time; /* know C? */
    Unprecedented performance: Nothing ever ran this slow before.
    Any sufficiently advanced bug is indistinguishable from a feature.
    Real Programmers confuse Halloween and Christmas, because dec 25 == oct 31.
    The best way to accelerate an IBM is at 9.8 m/s/s.
    recursion (re - cur' - zhun) n. 1. (see recursion)

  9. #9
    Registered User
    Join Date
    May 2004
    Posts
    164
    Not sure what you mean Cactus.. I think I see your point, it may have been a fluke with the null terminator, however, the way I did my calcs it appears to work. When you say C++ strings are you referring to CString?

    Please don't think I'm being snide, I like your idea about bullet proofing the code. Objective return is my goal. I think your point was that I am still returning a null terminator at the end of a string either way. But what I am trying to figure out is, if I join strings do I carry a single null terminator and dump the rest? Or do they all carry along with the new string? Trying to effort that now.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How do i un-SHA1 hash something..
    By willc0de4food in forum C Programming
    Replies: 4
    Last Post: 09-14-2005, 06:59 AM
  2. Half-life SDK, where are the constants?
    By bennyandthejets in forum Game Programming
    Replies: 29
    Last Post: 08-25-2003, 12:58 PM
  3. comparing fields in a text file
    By darfader in forum C Programming
    Replies: 9
    Last Post: 08-22-2003, 09:21 AM
  4. I'm having a problem with data files.
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 05-14-2003, 10:40 PM
  5. String sorthing, file opening and saving.
    By j0hnb in forum C Programming
    Replies: 9
    Last Post: 01-23-2003, 01:18 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21