利用sprintf和sscanf实现十六进制和十进制之间的相互转换【转】
程序员文章站
2024-03-17 13:26:04
...
(转自:https://blog.csdn.net/stpeace/article/details/13168851)
#include <stdio.h>
int main()
{
char s[100] = {0};
sprintf(s, "%x", 15);
printf("%s\n", s); // f
char str[100] = "f";
int i = 0;
sscanf(str, "%x", &i);
printf("%d\n", i); // 15
return 0;
}
好,再来看一个程序,加深印象:
#include <stdio.h>
#include <limits.h>
int main()
{
char s[100] = {0};
sprintf(s, "%x", INT_MAX);
printf("%s\n", s); // 7fffffff
char str[100] = "7fffffff";
int i = 0;
sscanf(str, "%x", &i);
printf("%d\n", i); // 2147483647
return 0;
}
下一篇: 编程练习:进制转换(10→2)