Common base conversions in C language

Article directory

  • 1. Convert decimal numbers to binary and octal numbers
  • 2. Convert decimal numbers to hexadecimal numbers
  • 3. Convert binary numbers and octal numbers to decimal numbers
  • 4. Convert hexadecimal numbers to decimal numbers

foreword

The most common base in life is decimal, and there is a type of programming problem that requires converting decimal to other bases. This blog will mainly talk about several common base conversion problems in C language.


1. Convert decimal numbers to binary and octal numbers

      Method: For example, to convert the decimal number 100 into a binary number is to divide 100 by 2 to get the quotient and remainder, and then divide the quotient by 2 continuously until the quotient is 0. Record the remainder in order, and then output the remainder in reverse order, which is the binary result. The octal system is the same, the quotient is continuously divided by 8, and finally the quotient is 0, the remainder is recorded in order, and then the remainder is output in reverse order, which is the result of the octal system. This method can also be extended to any base to be converted into two to nine.

The graphics are as follows:

 code show as below:

#include <stdio.h>
int main()
{
    int x, n;
    scanf("%d %d", &x, &n);//x是要转换的十进制数,n为要转换成的进制
    int arr[100] = { 0 };//创建一个足够大的数组来存放余数
    int i = 0,j = 0;
    while (x)
    {
        arr[i++] = x % n;//存放余数,i最后的值正好是余数的个数
        x /= n;
    }
    for (j = i - 1; j >= 0; j--)
    {
        printf("%d", arr[j]);//余数倒序输出
    }
    printf("\n");
    return 0;
}

2. Convert decimal to hexadecimal

       Converting decimal to hexadecimal also requires methods such as continuously dividing the quotient by 16 and outputting in reverse order, but since characters such as A\B\C\D\E\F will appear in hexadecimal, it is also necessary to add The obtained remainder is further converted. code show as below:

#include <stdio.h>
int main()
{
    int x;
    scanf("%d", &x);
    char arr[100] = { 0 };//因为十六进制中会出现A\B\C\D\E\F等字符,所以保存余数的数组类型应定义为char
    int i = 0, j = 0;
    while (x)
    {
        arr[i++] = x % 16;
        x /= 16;
    }
    for (j = 0; j < i; j++)
    {
        //将取出的余数转换为对应的字符
        switch (arr[j])
        {
        case 10:arr[j] = 'A'; break;
        case 11:arr[j] = 'B'; break;
        case 12:arr[j] = 'C'; break;
        case 13:arr[j] = 'D'; break;
        case 14:arr[j] = 'E'; break;
        case 15:arr[j] = 'F'; break;
        default:arr[j] += 48;
        }
    }
    for (j = i - 1; j >= 0; j--)
    {
        printf("%c", arr[j]);//逆序打印
    }
    printf("\n");
    return 0;
}

3. Convert binary numbers and octal numbers to decimal numbers

Method: It is necessary to take out each bit of the input number, multiply it with the weight of this bit, and finally add the result.

code show as below:

#include <stdio.h>
#include <math.h>
int main()
{
	int n = 0, sum = 0, i = 0, x = 0;
	scanf("%d %d", &x, &n);//x为输入的二进制或八进制的数,n为进制。
	while (x)
	{
		sum += (x%10)*pow(n, i++);//x%10将x的每一位取出,再与这个位上的权重相乘。
		x /= 10;
	}
	printf("%d\n", sum);
	return 0;
}

4. Convert hexadecimal numbers to decimal numbers

       Hexadecimal contains characters such as A\B\C\D\E\F, so it can be regarded as inputting a string when inputting, and it is necessary to convert each character into the corresponding number and standard first The weights on are multiplied and added together to get the final value.

code show as below:

#include <stdio.h>
#include <string.h>
#include <math.h>
int main()
{
    char arr1[100] = { 0 };
    scanf("%s", arr1);
    int len = strlen(arr1);
    int i = 0,  sum = 0;
    for (i = 0; i<len; i++)
    {
        int temp = 0;
        switch (arr1[i])
        {
        case 'A':temp = 10; break;
        case 'B':temp = 11; break;
        case 'C':temp = 12; break;
        case 'D':temp = 13; break;
        case 'E':temp = 14; break;
        case 'F':temp = 15; break;
        default:temp = arr1[i] - '0'; break;
        //将各个位上的字符转换为对应的数字
        }
        sum += temp * pow(16, len-i-1);//各个位上的权重相乘再相加
    }
    printf("%d\n", sum);
    return 0;
}

Guess you like

Origin blog.csdn.net/m0_74265792/article/details/129961737