Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何在uin8\u t类型的结构成员中复制mac地址字符串_C_Printf_Structure - Fatal编程技术网

如何在uin8\u t类型的结构成员中复制mac地址字符串

如何在uin8\u t类型的结构成员中复制mac地址字符串,c,printf,structure,C,Printf,Structure,使用snprintf函数获得的意外MAC地址值 为什么我会得到“意外的mac地址值。我有大字符串(unsigned char data[data_LEN])来解析mac地址并将其复制到结构成员。我得到的是完全不同的字符串。请对此提供帮助,谢谢 输入数据字符串: unsigned char data[512] = "its-STRING: 18 22 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 AC 12 00 20 00 00 00 C8 8C

使用snprintf函数获得的意外MAC地址值

为什么我会得到“意外的mac地址值。我有大字符串(unsigned char data[data_LEN])来解析mac地址并将其复制到结构成员。我得到的是完全不同的字符串。请对此提供帮助,谢谢

输入数据字符串:

unsigned char data[512] = "its-STRING: 18 22 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 AC 12 00 20 00 00 00 C8 8C DF 9D 57 12 20 00 00 00 29 \n";
程序的输出

Parsed Mac from string =  8C DF 9D 57 12 20
copied MacAddress == 32:30:3a:33:38:00
从上面提到的字符串中,我必须提取mac地址“8CDF9D57 12 20”,然后我必须将此mac地址复制到以下结构中

typedef struct my_stuct_s{
   uint8_t mac_addr[18];
  }my_stuct_t;
下面是我如何编码的

#define PARSE_OFFSET 89
#define END_OFFESET 19
#define DATA_LEN 512
#define ADDR_LEN 6


typedef struct my_stuct_s{
   uint8_t mac_addr[ADDR_LEN];
   uint8_t item;
}my_stuct_t;


int main()
{
  unsigned char data[DATA_LEN] = "its-STRING: 18 22 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 AC 12 00 20 00 00 00 C8 8C DF 9D 57 12 20 00 00 00 29 \n";
  unsigned char strv6[ADDR_LEN];

  unsigned char *data1 = NULL;

  my_stuct_t shm_memory;

  memset(strv6, 0,sizeof(strv6));
  memset(&shm_memory, 0,sizeof(my_stuct_t));

  if ((strcmp(data, "") ) != 0)
  {
    data1 = &data[0];
    data1 = data1 + PARSE_OFFSET;
    snprintf(strv6, END_OFFESET,"%s\n", data1);

    printf("Parsed Mac from string = %s\n", strv6);
    snprintf((char *)&shm_memory.mac_addr, ADDR_LEN,
            "%02x:%02x:%02x:%02x:%02x:%02x\n",
            strv6[0], strv6[1],
            strv6[2], strv6[3],
            strv6[4], strv6[5]);
 printf("copied MacAddress == %02x:%02x:%02x:%02x:%02x:%02x\n ",
          shm_memory.mac_addr[0],
          shm_memory.mac_addr[1],
          shm_memory.mac_addr[2],
          shm_memory.mac_addr[3],
          shm_memory.mac_addr[4],
          shm_memory.mac_addr[5]);
  }
  else
    printf("\n empty string");


  return 0;
}
  • 解析偏移量从89开始,要复制的字符数为18

    #define PARSE_OFFSET 89
    #define END_OFFESET 19
    
    应该是

    #define PARSE_OFFSET 90
    #define END_OFFESET 18
    
  • 存储地址的字符数组的长度应为19

    unsigned char strv6[19];
    
  • 您需要使用
    sscanf
    而不是
    sprintf
    ,如下所示

    sscanf(strv6, "%02x %02x %02X %02x %02x %02x\n",
      &shm_memory.mac_addr[0],
      &shm_memory.mac_addr[1],
      &shm_memory.mac_addr[2],
      &shm_memory.mac_addr[3],
      &shm_memory.mac_addr[4],
      &shm_memory.mac_addr[5]);
    
  • 解析偏移量从89开始,要复制的字符数为18

    #define PARSE_OFFSET 89
    #define END_OFFESET 19
    
    应该是

    #define PARSE_OFFSET 90
    #define END_OFFESET 18
    
  • 存储地址的字符数组的长度应为19

    unsigned char strv6[19];
    
  • 您需要使用
    sscanf
    而不是
    sprintf
    ,如下所示

    sscanf(strv6, "%02x %02x %02X %02x %02x %02x\n",
      &shm_memory.mac_addr[0],
      &shm_memory.mac_addr[1],
      &shm_memory.mac_addr[2],
      &shm_memory.mac_addr[3],
      &shm_memory.mac_addr[4],
      &shm_memory.mac_addr[5]);