Lanqiao Cup Test Questions Basic Practice Hexadecimal to Decimal (C Language)

Problem Description

  Input a positive hexadecimal number string with no more than 8 digits from the keyboard, convert it to a positive decimal number and output it.
  Note: 10~15 in hexadecimal numbers are represented by uppercase English letters A, B, C, D, E, and F respectively.

Sample input

FFFF

Sample output

65535

 

#include<bits/stdc++.h>
using namespace std;

int main()
{
	char a[16]={'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};
	char b[10];
	int k=0,i;
	int n;
	
	scanf("%d",&n);
	if(n==0)
	printf("0");
	else
	{
		while(n)
		{
			b[k++]=a[n%16];
			n=n/16;
		}
		for(i=k-1;i>=0;i--)
		printf("%c",b[i]);
	}
}

It will be much faster to directly print the table from decimal to hexadecimal , so you can save the corresponding ones again.

Guess you like

Origin blog.csdn.net/with_wine/article/details/114974727