我正在尝试将数组转换为十六进制,然后将其放入一个字符串变量中。在以下循环中printf工作正常,但我不能正确使用sprintf。如何将十六进制值作为ASCII填充到数组中?
static unsigned char digest[16];
static unsigned char hex_tmp[16];
for (i = 0; i < 16; i++) {
printf("%02x",digest[i]); <--- WORKS
sprintf(&hex_tmp[i], "%02x", digest[i]); <--- DOES NOT WORK!
}
答案 0 :(得分:11)
static unsigned char digest[16];
static char hex_tmp[33];
for (i = 0; i < 16; i++) {
printf("%02x",digest[i]); <--- WORKS
sprintf(&hex_tmp[i*2],"%02x", digest[i]); <--- WORKS NOW
}
答案 1 :(得分:9)
也许你需要:
&hex_tmp[i * 2]
还有一个更大的阵列。
答案 2 :(得分:-2)
存储为数字的字符与字符串不同:
unsigned char i = 255;
unsigned char* str = "FF";
unsigned char arr1[] = { 'F', 'F', '\0' };
unsigned char arr2[] = { 70, 70, 0 };