十六进制数转十进制

#include<stdio.h>

int hexToDec(char *ch)
{
	int flag=0;
	int hex = 0;
	int n;
	while (*ch != '\0')
	{
		if (*ch<='9'&&*ch>='0')
		{
			flag = 1;
			n = *ch - '0';
			hex = hex * 16 + n;
			++ch;
		}
		else if (*ch<='f'&&*ch>='a')
		{
			flag = 1;
			n = *ch - 'a' + 10;
			hex = hex * 16 + n;
			++ch;
		}
		else if (*ch<='F'&&*ch>='A')
		{
			flag = 1;
			n = *ch - 'A' + 10;
			hex = hex * 16 + n;
			++ch;
		}
		else
		{
			if (flag)
				break;
			else
				++ch;
		}
	}
	return hex;
}

int main()
{
	char hex[10];
	int decimal = 0;
	printf("Input a Hex numbers: ");
	gets(hex);
	decimal=hexToDec(hex);
	printf("The decimal value is : %d\n",decimal);
	return 0;
}

    原文作者:进制转换
    原文地址: https://blog.csdn.net/nyist327/article/details/25281125
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞