我正在使用这种方法将十六进制字符串转换为字节数组。代码是正确的。在编译此代码时,我收到低于编译警告。有什么办法可以解决吗?
/* test.c */
#include <stdio.h>
#include <stdint.h>
#include <string.h>
#include <stdlib.h>
int main(int argc, char *argv[])
{
if (argc < 2)
{
printf("Usage: ./test <input hex string>\n");
return 0;
}
char *hexstring = argv[1];
printf("hextring:%s\n", hexstring);
uint8_t str_len = strlen(hexstring);
printf("length:%d\n", str_len);
uint8_t array_size = str_len / 2;
printf("array_size:%d\n", array_size);
uint8_t *input = (uint8_t *)calloc(array_size, sizeof(uint8_t));
for (int i = 0; i < array_size; i++)
{
sscanf(hexstring, "%02x", &input[i]);
hexstring = hexstring + 2;
}
for (int i = 0; i < array_size; i++)
{
printf("input[%d]:[%.2x]\n", i, input[i]);
}
return 0;
}
编译警告:
gcc test.c -o test
test.c:24:34: warning: format specifies type 'unsigned int *' but the argument has type 'uint8_t *' (aka 'unsigned char *') [-Wformat]
sscanf(hexstring, "%02x", &input[i]);
~~~~ ^~~~~~~~~