使用'sprintf'将十六进制转换为字符串

Dbe*_*erg 4 c

我试图将数组转换为十六进制,然后将其放入一个字符串变量.在以下循环中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!
}
Run Code Online (Sandbox Code Playgroud)

use*_*313 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
}
Run Code Online (Sandbox Code Playgroud)


lep*_*pie 9

也许你需要:

&hex_tmp[i * 2]
Run Code Online (Sandbox Code Playgroud)

还有一个更大的阵列.