(转自: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;
}
来源:oschina
链接:https://my.oschina.net/u/4362486/blog/4270307