从另一个数组填充C中的char数组

时间:2020-12-05 16:03:06

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

我试图将内容从一个char数组复制到另一个char数组,下面是我的代码,

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 char数组,

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

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

编辑:源内容是字母数字(例如)11,2F,3A,BB,E1也是可能的

3 个解决方案

#1


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.

假设ch总是要复制两个字符。

#2


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

而不是strtok,可以使用sscanf解析内容。 %2hhX将扫描两个十六进制字符并将结果存储在char中。 ,将扫描任何空格和逗号。 %n将捕获扫描处理的字符数,以添加到ch以解析内容中的下一个字段

#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;
}

#3


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):

据我所知,你必须考虑十六进制表示,这可以通过使用带有基数16的strtol来完成(OP给出输入“11,2F,3A,BB,E1”作为示例):

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, ",");
}

#1


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.

假设ch总是要复制两个字符。

#2


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

而不是strtok,可以使用sscanf解析内容。 %2hhX将扫描两个十六进制字符并将结果存储在char中。 ,将扫描任何空格和逗号。 %n将捕获扫描处理的字符数,以添加到ch以解析内容中的下一个字段

#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;
}

#3


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):

据我所知,你必须考虑十六进制表示,这可以通过使用带有基数16的strtol来完成(OP给出输入“11,2F,3A,BB,E1”作为示例):

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, ",");
}