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

数制转换:a进制转换为b进制

程序员文章站 2024-03-15 12:49:17
...
#include<stdio.h>
#include<string.h>
int main()
{
	long int a, b;
	char num[100];
	while (scanf("%d%s%d", &a, num, &b) != EOF)
	{
		int length = strlen(num), c = 1; int temp=0;
		for (int i = length - 1; i >= 0; i--)
		{
			int x;
			if (num[i] >= '0'&&num[i] <= '9')
				x = num[i] - '0';
			else if (num[i] >= 'a'&&num[i] <= 'f')
				x = num[i] - 'a' + 10;
			else
				x = num[i] - 'A' + 10;
			temp += x * c;
			c *= a;
		}
		char buf[100]; int size = 0;
		do {
			int x = temp % b;
			if (x >= 10)
				buf[size++] = x - 10 + 'A';
			else
				buf[size++] = x + '0' ;
			temp = temp / b;
		} while (temp != 0);
		for (int i = size - 1; i >= 0; i--)
			printf("%c", buf[i]);
		printf("\n");
	}
	return 0;
}