1

I am trying to copy content from one char array to another char array and below is my code,

char dest[100]; //destination array
char content[100]; //Which will be "11,22,33,44,55" - source array

//Split source array with comma delimiter
char *ch ;
ch  = strtok(content, ",");
while (ch != NULL) {
  printf("%s\n", ch); //prints each entry seperated by comma 
  ch = strtok(NULL, " ,");
  //Code to copy content to dest ?
}

i want to populate dest char array with below content,

dest[0] = 11 dest[1] = 22 dest[2] = 33 dest[3] = 44 dest[4] = 55

I have tried below with no luck,

memcpy(dest, ch, 1);
strcpy(dest,ch);

How can i do this?

EDIT : The source content is alpha-numerical (e.g) 11,2F,3A,BB,E1 is alos possible

5
  • 2
    Can you please elaborate on the problems you have? How doesn't e.g. strcpy work? What is the result you get, and what did you expect? Commented Sep 15, 2017 at 9:11
  • 1
    Oh and you do the copying before the call to strtok in the loop? Commented Sep 15, 2017 at 9:13
  • 1
    char dest[100]; --> char dest[50][100];.. strcpy(dest[index++], ch); Commented Sep 15, 2017 at 9:25
  • 1
    what is the max value of a token? Commented Sep 15, 2017 at 9:42
  • 3
    It's a bit unclear what you want. Please provide a minimal reproducible example, e.g: char content[100] = "65,66,67,68"; and dest array should then contain "ABCD". Commented Sep 15, 2017 at 9:47

3 Answers 3

1

Try this:

int  i = 0;
while (ch != NULL) {
  printf("%s\n", ch);
  dest[i++] = ch[0];
  dest[i++] = ch[1];
  ch = strtok(NULL, " ,");
}

assuming that ch has always two characters to copy.

Sign up to request clarification or add additional context in comments.

Comments

1

instead of strtok, content could be parsed using sscanf. %2hhX will scan two hex characters and store the result in a char. , will scan any whitespace and a comma. %n will capture the number of characters processed by the scan to add to ch to parse the next field in content

#include <stdio.h>
#include <stdlib.h>

#define SIZE 100

int main( void) {
    char dest[SIZE]; //destination array
    char content[SIZE] = "11,22,33,44 , 55,C,2F,3A,BB,E1";
    char *ch = content;
    int span = -1;
    int each = 0;

    while ( 1 == sscanf ( ch, "%2hhX ,%n", &dest[each], &span)) {
        printf ( "%hhX\n", dest[each]);
        if ( span == -1) {//failed to scan a comma
            break;
        }
        ch += span;//advance ch to next field in content
        span = -1;//reset span
        each++;
        if ( each >= SIZE) {
            break;
        }
    }
    return 0;
}

Comments

1

As far as I understand you have to consider hex representations, which can be done by using strtol with base 16 (the OP gave input "11,2F,3A,BB,E1" as example):

int i = 0;
char *ch = strtok(content, ",");
while (ch != NULL) {
    printf("%s\n", ch); //prints each entry seperated by comma 
    dest[i++] = (char)strtol(ch, NULL, 16);   // number will be 11, 22, 33 etc.
    ch  = strtok(NULL, ",");
}

5 Comments

Huh? In OP code, dest[] is an array of char, and strtoul() returns an unsigned long....
You still have the same problem. Why are you storing the long value returned by strtol() in a char? OP question is unclear, but so is this answer.
@David: Changed it. Results larger than 0xFF will be truncated. If OP wants long, then the array's declaration has to be changed.
We don't even know if OP intends 11,2F,3A,BB,E1 to be hex values. And why would you convert a hex string to a long value only to store it in a char anyway?
@David: Because it's a simple solution. I only assume that the OP wants to convert hex values, that's how I interpret the original question including the edit.

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.