C语言十六进制和十进制互转

#include
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;
}

lbo@donglebuild2:~/lin/test$ ./a.out    
dec:32
hex:0x0020
----------------------------------------------
hex:0093
dec:147
lbo@donglebuild2:~/lin/test$ 

你可能感兴趣的:(linux,c,字符串)