欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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$