C语言十六进制和十进制互转
程序员文章站
2024-03-17 12:51:34
...
#include<stdio.h>
int main(void)
{
int i;
char buf[64] = {0};
//scanf("%d",&i);
i = 32;
printf("dec:%d\n",i);
sprintf(buf,"0x%04x",i);
printf("hex:%s\n",buf);
printf("----------------------------------------------\n");
char str[100] = "0093";
printf("hex:%s\n", str);
i = 0;
sscanf(str, "%x", &i);
printf("dec:%d\n", i);
return 0;
}
[email protected]:~/lin/test$ ./a.out
dec:32
hex:0x0020
----------------------------------------------
hex:0093
dec:147
[email protected]:~/lin/test$