[c language] detailed code implementation of input value conversion (binary, decimal, hexadecimal, etc.)

Computer base and base conversion

Multiple bases of the computer:

Today, I'd like to share the conversion between binary, decimal and hexadecimal. It's full of dry goods.

First of all, I'll give you a brief introduction to the common hexadecimal systems in the computer field: binary, octal, decimal and hexadecimal.

Binary:

Every two into one, there are only 0 and 1 in the number.

Where S represents one digit, k is the position of the digit, and the base is 2.

octal number system:

Every eight into one, the number contains 0, 1, 2, 3, 4, 5, 6 and 7.
Where S represents a number, k is the position of the number, and the base number is 8.

decimal system:

Every decimal one, the number contains 0, 1, 2, 3, 4, 5, 6, 7, 8 and 9, where S represents 1 number, k is the position of the number, and the base is 10.
Where S represents a number, k is the position of the number, and the base number is 10.

hexadecimal:

Every 16 into one, because 10-15 with 16 as the base cannot be represented by A single number, it is replaced by English letters. 10 is represented by A, 11 by B, 12 by C, 13 by D and 14 by F. So hex contains: 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, A,B,C,D,F.

After we have a simple understanding of various hexadecimals, the problem of how to convert them to each other has emerged. Next, we use c language code to realize the conversion between them one by one. There are 12 mutual transformations between them. (four common ones are divided here)

Code implementation (body):

Binary to decimal:

The calculation method is to multiply the N-power of 2 from right to left, n starts from zero, and the ^ symbol represents the power.

For example: 111 (omitting the previous 0), its decimal expression is 22 + 21 + 2 ^ 0 = 7

We only need to find the symbol '1' in the array arr and calculate the power of 2 according to its position. The symbol '0' does not need to be found and does not participate in the calculation.

Code implementation:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	char arr[1001];//Create an array char arr[1001] to receive the number of hexadecimals to be converted. (note that it is an array of char type)
	while (gets(arr) != NULL)
	{
		int len, i, sum = 0, num, j;//Initialize variables. num is the value of each bit, and sum is the last sum of each bit.
		
		len = strlen(arr);//len is the length of the input character array.
		for (i = 0; i < len; i++)
		{
			num = 1;
			if (arr[i] == '1')//If the bit is 1, then the power operation is performed. If it is 0, then it doesn't matter (0 doesn't participate in the calculation)
			{
				for (j = 1; j <= len - i - 1; j++)//j is the number of powers of 2 on each bit.
				{
					num = num * 2;
				}
				sum = sum + num;//sum is the last decimal value.
			}
		}
		printf("%d\n", sum);	
	}
	return 0;
}

Decimal to binary:

Decimal to binary is the inverse process of binary to decimal.

Take 10 as an example.

10 / 2 = 5 (remainder is 0)

5 / 2 = 2 (the remainder is 1)

2 / 2 = 1 (remainder is 0)

1 / 2 = 0 (the remainder is 1) ends.

So the final 1010 is the binary expression of 10.

Code implementation:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
	int n = 0;
	scanf("%d", &n);//Get a decimal number
	int i = 0;
	int arr[32];//Binary is represented by an integer array
	while (n)//The calculation can continue as long as n is not 0
	{
		i++;
		arr[i] = n % 2;//Assign a value to each bit of the array
		n = n / 2;//One bit is automatically eliminated after assignment
	}
	for (int j = i; j > 0; j--)
	{
		printf("%d", arr[j]);
	}
	return 0;
}

Hex to decimal

Multiply by the n-th power of 16 from right to left, and N starts from zero.

Example: 32

3x161+2x160=50

Code implementation:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#Include < math. H > / / don't forget to reference this library
int main()
{
	char a[20];//Enter hexadecimal number
	int b[20] = { 0 };//Convert hexadecimal number to int type
	int i, j, sum=0;//Don't forget to initialize the sum here.
	int c = 0;//Final decimal number
	gets(a);
	//Convert it from char type to int type and store it in array b [].
	while (a[sum] != '\0')
	{
		if ((a[sum] >= 'a') && (a[sum] <= 'f'))
		{
			b[sum] = a[sum] - 'a' + 10;
			sum++;
			continue;
		}
		if ((a[sum] >= 'A') && (a[sum] <= 'F'))
		{
			b[sum] = a[sum] - 'A' + 10;
			sum++;
			continue;
		}
			b[sum] = a[sum] - '0';
			sum++;
	}
   //Convert each bit to hexadecimal
	for(i = 0; i < sum; i++)Decimal to hexadecimal and hexadecimal to decimal are reciprocal
	{
		b[sum - 1 - i] = b[sum - 1 - i] * pow(16, i);
	}
	//Direct accumulation
	for (j = 0;j<sum;j++) 
	{
		c = c + b[j];
	}
	printf("%d", c);
	return 0;
}

Decimal to hexadecimal

Decimal to hexadecimal and hexadecimal to decimal are reciprocal

For example: 50

50 / 16 = 3 (the remainder is 2)

3 / 16 = 0 (the remainder is 3)

So its decimal system is 32

Code implementation:

#include<stdio.h>
int main()
{
	int a = 0;
	int arr[32] = { 0 };//Put the converted hexadecimal number into the array arr.
	int y = 0;
	scanf("%d", &a);//Gets a decimal number
	while (a != 0)
	{
		y++;
		arr[y] = a % 16;
		a = a / 16;
		if (arr[y] > 9)
		{
			arr[y] = 'A' + (arr[y] - 10);
		}
		else
		{
			arr[y] = '0' + arr[y];
		}
	}
	for (int i = y; i > 0; i--)
	{
		printf("%c", arr[i]);
	}
	return 0;
}
//The hexadecimal number stored in the array is reversed. When printing, you can directly print it backwards. What you print out is the hexadecimal number.
//It is the same as the decimal to binary conversion above.

If you think it's helpful, you can praise it and collect it.

Tags: C

Posted on Sun, 24 Oct 2021 03:06:39 -0400 by suresh1