1. 程式人生 > >譚浩強 習題7-16 十六進位制化十進位制

譚浩強 習題7-16 十六進位制化十進位制

#include<iostream>
using namespace std;
void jinzhi(char x[])
{
	int sum = 0, t, n = (int)strlen(x)-1;
	for (int i =n; i >= 0; i--)
	{
		if (x[i] >= '1'&&x[i] <= '9')
		{
			t = x[i] - '0';
			sum += t*(int)pow(16, n-i);
		}
		else if (x[i] >= 'A'&&x[i] <= 'F') {
			t = 10 + (x[i] - 'A');
			sum += t * (int)pow(16,n- i);
		}
		else if (x[i] >= 'a'&&x[i] <= 'f') {
			t = 10 + (x[i] - 'a');
			sum += t * (int)pow(16,n- i);
		}
	}
	cout << sum << endl;
}
int main()
{
	char x[1000];
	while (cin >> x)
	jinzhi(x);
	system("pause");
	return 0;
}