1

I am trying to parse a string in to an array with specific delimiters but its not working as expected. I tried a lot to get this working but failed.

Code i am using is below.

CODE TO PARSE

        char itemCode[] = "0xFF,0xAA,0xBB,0x00,0x01,0x04,0x90";
        char itemCodeToSend[34] = {0};
        char ** res  = NULL;
        char *  p    = strtok (itemCode, ",");
        int n_spaces = 0, i; 

        /* split string and append tokens to 'res' and 'itemCodeToSend' */

        while (p) {
          res = realloc (res, sizeof (char*) * ++n_spaces);

          if (res == NULL)
            exit (-1); /* memory allocation failed */

          res[n_spaces-1] = p; // Copying to char**
          strcpy(&itemCodeToSend[0],p);// Copying to Array

          p = strtok (NULL, ",");
        }

        /* realloc one extra element for the last NULL */

        res = realloc (res, sizeof (char*) * (n_spaces+1));
        res[n_spaces] = 0;

        /* print the result */

        for (i = 0; i < (n_spaces); ++i)
          printf ("res[%d] = %s\n", i, res[i]);

                    for (i = 0; i < 34; ++i)
          printf ("0x%02x",(unsigned)res[i]&0xffU);

        /* free the memory allocated */ 
        free (res); 

I am getting the below output for char** but not for char[]

res[0] = "0xFF";    itemCodeToSend[0] = "0x30";
res[1] = "0xAA";    itemCodeToSend[1] = "0x30";
res[2] = "0xBB";    itemCodeToSend[2] = "0x30";
res[3] = "0x00";    itemCodeToSend[3] = "0x30";
res[4] = "0x01";    itemCodeToSend[4] = "0x30";
res[5] = "0x04";    itemCodeToSend[5] = "0x30";
res[6] = "0x90";    itemCodeToSend[6] = "0x30";

Am i using right way to copy the extracted value to array?

13
  • 2
    'What i am doing wrong here?' - not debugging. Commented May 8, 2014 at 9:37
  • 1
    0x30 = ASCII for "0", if this helps you debug. Commented May 8, 2014 at 9:41
  • 2
    @JoachimPileborg He already handles that case with exit(-1). Commented May 8, 2014 at 9:45
  • 1
    @JoachimPileborg Indeed, pardon me. On Topic Vino: If you want the characters inside itemCodeToSend to represent the hex values in your string you need to somehow parse them (strtol as suggested below, sscanf might do aswell). At the moment you are just copying the first char (which is always a "0" cause all your entries start with "0x"). Commented May 8, 2014 at 9:48
  • 1
    @Vino NP, i think you got your answer bellow. Just a small trick to output 0x for your hexa: printf("%#02x", ...). Commented May 8, 2014 at 9:49

1 Answer 1

2

Looks like you want these lines to access itemCodeToSend.

                for (i = 0; i < 34; ++i)
      printf ("0x%02x",(unsigned)res[j]&0xffU);

Before that, you you write to itemCodeToSend it looks like you want to write the integer values, not their textual representation. And not to overwrite the same element in each loop.

Perhaps you need something along the lines of

itemCodeToSend[n_spaces-1] = strtol(p, 0, 16);

or similar, instead of

strcpy(&itemCodeToSend[0],p);// Copying to Array
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks chill. This is what i am looking for. I spent over 5 hours just in this. Much Appreciated

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.