how to convert array of uchar to string

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • therock112
    New Member
    • Nov 2006
    • 8

    how to convert array of uchar to string

    hello

    I am new to C and not sure how to go about getting the following accomplished:

    I have the following c code, under centos ver 3.4, using gnu gcc to compile:

    uchar sn[8];

    // readings

    sn[0] = 0x10;
    sn[1] = 0x2E;
    sn[2] = 0x22;
    sn[3] = 0x47;
    sn[4] = 0x00;
    sn[5] = 0x08;
    sn[6] = 0x00;
    sn[7] = 0x35;

    I want to convert the above array into an Ascii string, is there any simple way of achieving this in C?

    ultimately i want to do the following:

    query = "Insert into test (reading) values(sn)";
    // i want to get it formated as "Insert into test (reading) values ('3500080047222 E10')"
    // I understand that c will not insert the value of the array 'sn', unlike php or other glue languages...i am hoping there is some way to achieve this..

    do_this_query(q uery);

    any help would be appreciated.
  • therock112
    New Member
    • Nov 2006
    • 8

    #2
    any ideas, anyone???

    Comment

    • sivadhas2006
      New Member
      • Nov 2006
      • 142

      #3
      Hi,

      Can you post your full code?

      Can I know from where you are getting these values?

      Code:
      sn[0] = 0x10;
      sn[1] = 0x2E;
      sn[2] = 0x22;
      sn[3] = 0x47;
      sn[4] = 0x00;
      sn[5] = 0x08;
      sn[6] = 0x00;
      sn[7] = 0x35;
      So we can think to solve the problem differently.

      Regards,
      M.Sivadhas.

      Comment

      • therock112
        New Member
        • Nov 2006
        • 8

        #4
        Originally posted by sivadhas2006

        Can you post your full code?
        Sure.

        My following code is a direct copy from Dallas/Maxim's site, I have tried to incorporate the Mysql c api in it as well so I could directly send the measured temperature information to a sql table.

        for the fullsource code from dallas, please visit the following site to get their sdk
        :


        Following is my hacked c code, please bear in mind I am not a programmer, so I am pretty new to this stuff. For the original code from Dallas, you may want to see the temp.c from the above sdk.

        Code:
        #include <stdlib.h>
        #include <stdio.h>
        #include <mysql.h>
        #include <string.h>
        #include "ownet.h"
        #include "temp10.h"
        #include "findtype.h"
        
        // defines
        #define MAXDEVICES         20
        
        // global serial numbers
        uchar FamilySN[MAXDEVICES][8];
        
        uchar *fsn; // not part of original dallas code
        uchar *fsnid; // not part of original dallas code
        
        // MySQL db info
        // #define user     'log'
        // #define pass     '4321'
        // #define hostname 'localhost'
        // #define dbname   'tlogs'
        
        // variables
        int family_code;
        
        //----------------------------------------------------------------------
        //  Main Test for DS1920/DS1820 temperature measurement
        //
        int main(int argc, char **argv)
        {
        
        // MySQL stuff
        
           MYSQL mysql; // not part of original dallas code
           MYSQL_RES *res; // not part of original dallas code
           MYSQL_ROW row; // not part of original dallas code
        
           char query[200]; // not part of original dallas code
           mysql_init(&mysql); // not part of original dallas code
        
        // OneWire Stuff
        
           float current_temp;
           int i = 0;
           int NumDevices=0;
           int portnum = 0;
        
           //----------------------------------------
           // Introduction header
           printf("\n/---------------------------------------------\n");
        
           printf("  Press any CTRL-C to stop this program.\n\n");
           printf("  Output [Serial Number(s) ........ Temp1(F)] \n\n");
        
           // check for required port name
           if (argc != 1)
           {
              printf("1-Wire Net name required on command line!\n"
                     " (example: \"COM1\" (Win32 DS2480),\"/dev/cua0\" "
                     "(Linux DS2480),\"1\" (Win32 TMEX)\n");
              exit(1);
           }
        
        
           // attempt to acquire the 1-Wire Net
           if((portnum = owAcquireEx("/dev/ttyS0")) < 0)
           {
              OWERROR_DUMP(stdout);
              exit(1);
           }
        
           // success
           printf("Port opened: %s\n",argv[1]);
        
           // open connection to mysql server
           mysql_real_connect(&mysql,"localhost","log","4321","tlogs",0,NULL,0);
           printf("\n Connection opened to MySQL server\n");
        
        
           // Find the device(s)
           NumDevices = FindDevices(portnum, &FamilySN[0], 0x10, MAXDEVICES);
        
        printf("\n Got here 1 \n"); // simple debug 
        
           // Write the discovered device info to MySQL server
           if (NumDevices>0)
           {
        
        printf("\n Got here 2 \n"); // simple debug 
        
              for (i = 0; i < NumDevices; i++)
              {
        
        printf("\n Got here 3 \n"); // simple debug 
        
        
        	fsn = FamilySN[i]; // not part of original dallas code
        	fsnid = &FamilySN[i][0];  // not part of original dallas code
        	
        	// query = "INSERT into rawtemp (id, deviceid, devicetype, temp, logtime, logdate) VALUES(,";
        
        printf("Serial Number %i", strlen(FamilySN[i])); // not part of original dallas code
        
        	//  + fsn + "," + fsnid + "," + "22.10,,)"; 
        
        	// sprintf(query,query);
        	//sprintf(query,"INSERT into rawtemp (id, deviceid, devicetype, temp, logtime, logdate) VALUES(,fsn,fsnid,21,curtime(),curdate())");
        
        printf("\n Got here 4 \n");
        
        // printf("Query = %s\n", query);
        
        	mysql_real_query(&mysql,query,(unsigned int)strlen(query));
        //	query = "INSERT into rawtemp (id, deviceid, devicetype, temp, logtime, logdate) VALUES(,fsn,'10',,,);
        //	mysql_query(&mysql, query);
        
        printf("\n Got here 5 \n");
        
        //	res = mysql_use_result(&mysql);
        //	printf("\n Wrote record %i to MySQL server\n",i);
              }
        
             // close and reslease connection mysql server
             mysql_free_result(res);
             printf("\n Connection close to MySQL server\n");
        
           }
        
           if (NumDevices>0)
           {
              printf("\n");
              printf("Device(s) Found: \n");
              for (i = 0; i < NumDevices; i++)
              {
                 PrintSerialNum(FamilySN[i]);
                 printf("\n");
              }
              printf("\n\n");
        
        
              // (stops on CTRL-C)
              do
              {
                 // read the temperature and print serial number and temperature
                 for (i = 0; i < NumDevices; i++)
                 {
        
                    if (ReadTemperature(portnum, FamilySN[i],&current_temp))
                    {
                       PrintSerialNum(FamilySN[i]);
                       printf("%5.1f \n", current_temp);
                    }
                    else
                       printf("     Error reading temperature, verify device present:%d\n",
                               (int)owVerify(portnum, FALSE));
                 }
                 printf("\n");
              }
              while (!key_abort());
           }
           else
              printf("\n\n\nERROR, device DS1920/DS1820 not found!\n");
        
           // release the 1-Wire Net
           owRelease(portnum);
           printf("Closing port %s.\n", argv[1]);
           exit(0);
        
           return 0;
        }
        I hope this is not too confusing to understand. I am familiar with php, but php has extensive string handling capabilities. I am trying to figure out how to achieve some of the simplest string manipulation in C, so far it seems like its much more complex than in php.

        Also the following :

        Code:
        sn[0] = 0x10;
        sn[1] = 0x2E;
        sn[2] = 0x22;
        sn[3] = 0x47;
        sn[4] = 0x00;
        sn[5] = 0x08;
        sn[6] = 0x00;
        sn[7] = 0x35;
        is the address of a temperature sensor.

        any ideas would be helpful to get this code to read temperature from the one wire bus and write them to a mysql table.

        thanks.

        Comment

        • horace1
          Recognized Expert Top Contributor
          • Nov 2006
          • 1510

          #5
          a simple way to convert your array into a string would be to use sprintf(), e.g. using the %x conversion specification to get hexadecimal output
          Code:
          #include <stdio.h>
          
          int main()
          {
              int i, sn[8];
              sn[0] = 0x10;
              sn[1] = 0x2E;
              sn[2] = 0x22;
              sn[3] = 0x47;
              sn[4] = 0x00;
              sn[5] = 0x08;
              sn[6] = 0x00;
              sn[7] = 0x35;
              for(i=0; i<8;i++) printf("%x ", sn[i]);
              char text[20];
              // convert hex values in sn[] into a string in text[]
              sprintf(text,"%x%x%x%x%x%x%x%x",sn[7],sn[6],sn[5],sn[4],sn[3],sn[2],sn[1],sn[0]);
              printf("\ntext = '%s'\n", text);
          getchar();
          }
          a run of this gave
          10 2e 22 47 0 8 0 35
          text = '3508047222e10'

          Comment

          • therock112
            New Member
            • Nov 2006
            • 8

            #6
            Originally posted by horace1
            a simple way to convert your array into a string would be to use sprintf(), e.g. using the %x conversion specification to get hexadecimal output
            Code:
                char text[20];
                // convert hex values in sn[] into a string in text[]
                sprintf(text,"%x%x%x%x%x%x%x%x",sn[7],sn[6],sn[5],sn[4],sn[3],sn[2],sn[1],sn[0]);
                printf("\ntext = '%s'\n", text);
            getchar();
            }
            horace1, thats exactly what I was looking for.....thanks a million

            following is what I came up with, and it works!!!!


            sprintf(devicea ddress,"%02x%02 x%02x%02x%02x%0 2x%02x%02x",Fam ilySN[i][7],FamilySN[i][6],FamilySN[i][5],FamilySN[i][4],FamilySN[i][3],FamilySN[i][2],FamilySN[i][1],FamilySN[i][0]);

            sprintf(query, "INSERT into tlogs (id, deviceid, devicetype, temp, logtime, logdate) VALUES(0,'%s',' %x','%5.1f',cur time(), curdate())", deviceaddress,F amilySN[i][0],current_temp);

            hopefully this helps someone out there.......

            thanks again.

            therock

            Comment

            • therock112
              New Member
              • Nov 2006
              • 8

              #7
              Originally posted by therock112
              horace1, thats exactly what I was looking for.....thanks a million

              following is what I came up with, and it works!!!!


              sprintf(devicea ddress,"%02x%02 x%02x%02x%02x%0 2x%02x%02x",Fam ilySN[i][7],FamilySN[i][6],FamilySN[i][5],FamilySN[i][4],FamilySN[i][3],FamilySN[i][2],FamilySN[i][1],FamilySN[i][0]);

              sprintf(query, "INSERT into tlogs (id, deviceid, devicetype, temp, logtime, logdate) VALUES(0,'%s',' %x','%5.1f',cur time(), curdate())", deviceaddress,F amilySN[i][0],current_temp);

              hopefully this helps someone out there.......

              thanks again.

              therock
              Continuing the fun,

              I now want to do the reverse of course, read from mysql database, and copy the deviceid into a variable, resulting in the same format.

              when I receive the mysql result, the result is in string format, how do i go about converting those exact address such as 3500080047222E1 0 and correctly placing them in an array?

              Comment

              • therock112
                New Member
                • Nov 2006
                • 8

                #8
                Originally posted by therock112
                Continuing the fun,

                I now want to do the reverse of course, read from mysql database, and copy the deviceid into a variable, resulting in the same format.

                when I receive the mysql result, the result is in string format, how do i go about converting those exact address such as 3500080047222E1 0 and correctly placing them in an array?
                I have tried the following without much luck:

                Code:
                mysql includes;
                declare and init other varibles etc;
                
                int NumDevices =0;
                
                other mysql related initialization, not shown to keep things simple;
                
                sprintf(query, "SELECT distinct ucase(deviceid) from tlogs where devicetype='10'");
                
                mysql_real_query(&mysql,query,(unsigned int)strlen(query));
                
                res = mysql_use_result(&mysql);
                while((row = mysql_fetch_row(res)))
                {
                printf("\nTemperature Sensor ID's =  %s\n", row[0]); // this prints correctly.
                
                sscanf(row[0],"%02x%02x%02x%02x%02x%02x%02x%02x", arrayofuchars[NumDevices][7],arrayofuchars[NumDevices][6],arrayofuchars[NumDevices][5],arrayofuchars[NumDevices][4],arrayofuchars[NumDevices][3],arrayofuchars[NumDevices][2],arrayofuchars[NumDevices][1],arrayofuchars[NumDevices][0]);
                
                printf("Device id = %02x%02x%02x%02x%02x%02x%02x%02x\n", arrayofuchars[NumDevices][7],arrayofuchars[NumDevices][6],arrayofuchars[NumDevices][5],arrayofuchars[NumDevices][4],arrayofuchars[NumDevices][3],arrayofuchars[NumDevices][2],arrayofuchars[NumDevices][1],arrayofuchars[NumDevices][0]); // prints unexpected stuff
                
                NumDevices++;
                }
                after the sscanf, i was hoping to get this into the array:

                arrayofuchars[0][7] = 0x35;
                arrayofuchars[0][6] = 0x00;
                arrayofuchars[0][5] = 0x08;
                arrayofuchars[0][4] = 0x00;
                arrayofuchars[0][3] = 0x47;
                arrayofuchars[0][2] = 0x22;
                arrayofuchars[0][1] = 0x2E;
                arrayofuchars[0][0] = 0x10;

                but I am sure, my lack of knowledge of c and its functions, operators etc etc is the cause of my hair pulling and screaming and frustration!!!

                any help would be appreciated.

                thanks.

                Comment

                • horace1
                  Recognized Expert Top Contributor
                  • Nov 2006
                  • 1510

                  #9
                  Originally posted by therock112
                  I have tried the following without much luck:

                  Code:
                  sscanf(row[0],"%02x%02x%02x%02x%02x%02x%02x%02x", arrayofuchars[NumDevices][7],arrayofuchars[NumDevices][6],arrayofuchars[NumDevices][5],arrayofuchars[NumDevices][4],arrayofuchars[NumDevices][3],arrayofuchars[NumDevices][2],arrayofuchars[NumDevices][1],arrayofuchars[NumDevices][0]);
                  
                  }
                  but I am sure, my lack of knowledge of c and its functions, operators etc etc is the cause of my hair pulling and screaming and frustration!!!

                  any help would be appreciated.

                  thanks.
                  you need to pass the address of the elements of array arrayofuchars[][] to scanf using the addressof operator &, e.g.
                  Code:
                  sscanf(row[0]"%02x%02x%02x%02x%02x%02x%02x%02x", &arrayofuchars[NumDevices][7],&arrayofuchars[NumDevices][6],&arrayofuchars[NumDevices][5],&arrayofuchars[NumDevices][4],&arrayofuchars[NumDevices][3],&arrayofuchars[NumDevices][2],&arrayofuchars[NumDevices][1],&arrayofuchars[NumDevices][0]);

                  Comment

                  • therock112
                    New Member
                    • Nov 2006
                    • 8

                    #10
                    Originally posted by horace1
                    you need to pass the address of the elements of array arrayofuchars[][] to scanf using the addressof operator &, e.g.
                    Code:
                    sscanf(row[0]"%02x%02x%02x%02x%02x%02x%02x%02x", &arrayofuchars[NumDevices][7],&arrayofuchars[NumDevices][6],&arrayofuchars[NumDevices][5],&arrayofuchars[NumDevices][4],&arrayofuchars[NumDevices][3],&arrayofuchars[NumDevices][2],&arrayofuchars[NumDevices][1],&arrayofuchars[NumDevices][0]);
                    I get some strange result, most of characters are 0's except the last 2 digits which gets converted correctly.....n ot sure why

                    when row[0] is 36000800402AE11 0
                    arrayofuchars[NumDevices] has 000000000000001 0

                    and it does this for all items, not sure why only the 10 gets converted correctly.

                    my exact syntax is as follows:

                    Code:
                    sscanf(row[0],"%02X%02X%02X%02X%02X%02X%02X%02X",&arrayofuchars[NumDevices][7],&arrayofuchars[NumDevices][6],&arrayofuchars[NumDevices][5],&arrayofuchars[NumDevices][4],&arrayofuchars[NumDevices][3],&arrayofuchars[NumDevices][2],&arrayofuchars[NumDevices][1],&arrayofuchars[NumDevices][0]);
                    I have tried upper case x and lowercase x, no difference.

                    arrayofuchars is defined as follows, arrayofuchars is global by the way:

                    Code:
                    uchar arrayofuchars[20][8];

                    Comment

                    • horace1
                      Recognized Expert Top Contributor
                      • Nov 2006
                      • 1510

                      #11
                      I think the problem is that you have defined the array arrayofuchars[20][8] as being uchar (which I assume is unsigned char). The %02x conversion specification to sscanf assumes the corresponding parameter is int* (pointer to int). Try defining arrayofuchars[20][8] as
                      Code:
                      int arrayofuchars[20][8];
                      and see if it works. You can then copy the values into a uchar array later.

                      Comment

                      • therock112
                        New Member
                        • Nov 2006
                        • 8

                        #12
                        Originally posted by horace1
                        I think the problem is that you have defined the array arrayofuchars[20][8] as being uchar (which I assume is unsigned char). The %02x conversion specification to sscanf assumes the corresponding parameter is int* (pointer to int). Try defining arrayofuchars[20][8] as
                        Code:
                        int arrayofuchars[20][8];
                        and see if it works. You can then copy the values into a uchar array later.
                        Yes it works!!!

                        now, how do i go about copying int type array items into unsigned char type array?

                        is the following legit syntax?

                        arrayofuchars[deviceid][index] = arrayofints[deviceid][index];

                        or

                        arrayofuchars[deviceid][index] = (unsigned char) arrayofints[deviceid][index];

                        Comment

                        Working...