Home  >  Article  >  How to achieve conversion between arbitrary bases in C language

How to achieve conversion between arbitrary bases in C language

清浅
清浅Original
2019-03-01 16:47:3627010browse

C语言实现任意进制间的转换可以以十进制为基准,先将需要转换的数字转换成十进制然后再将其转换成目标进制数,这样就实现了任意进制间的转换。

在C语言中我们可以实现任意进制的转换,接下来在文章中将分享如何实现C语言中的进制转化,具有一定的参考价值,希望对大家有所帮助

How to achieve conversion between arbitrary bases in C language

【推荐课程:C语言教程

实现任意进制间的转换思路:

以十进制数为基准,先将需要转换的数转换成十进制数,再将其转换成目标进制数

首先将涉及大于十进制的转换的数挑出来,利用ASCALL码将字符转换成整型或将整型转换成字符

以取余的方式,将十进制数转换成其他进制数,再按照按权展开的方式,将其他进制数转换成十进制数

How to achieve conversion between arbitrary bases in C language

代码如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#include <windows.h>
#define N 50
//进制转换
char * Ten_MoreThanTen(int, int);   //10进制数转换成10以上进制数的函数

int MoreThanTen_Ten(int, char []);  //10以上进制数转换成10进制数的函数

int Ten_LessThanTen(int, int);    //10进制数转换成10以内进制数的函数

int LessThanTen_Ten(int, int);    //10以内进制数转换成10进制数的函数

char array[N] = "\0";   //全局变量,用于存储转换后并且倒置了的数据

int LessThanTen_Ten(int before, int num)   //10以内转换10,参数brfore为初始进制,num为初始数值
{
    double result = 0.0;             //转换成10进制后的结果
    int i = 0;
    for(i = 0; num != 0; i++)        //利用for循环实现按权展开相加
    {
        result += pow(before, i) * (num % 10);
        num /= 10;
    }
    return (int)result;  //返回值为转换后的结果
}

int Ten_LessThanTen(int after, int num)  //10转换10以内,参数after为转换后的进制,num为10进制数值
{
    double result = 0.0;            //double类型因为pow函数的返回值为double类型
    int i = 0;
    for(i = 0; num != 0; i++)       //利用for循环实现连续取余
    {
        result += (num % after) * pow(10, i);
        num /= after;
    }
    return (int)result;
}

int MoreThanTen_Ten(int before, char num[]) //10以上转换10
{
    int i = 0;
    double result = 0.0;
    int length = strlen(num);
    for(i = length - 1; i >= 0; i--)
    {
//利用ASCALL码将所有元素转换成对应的整型
        if(&#39;A&#39; <= num[i] && num[i] <= &#39;Z&#39;)
            result += pow(before, length - i - 1) * (num[i] - 55);
        else if(&#39;a&#39; <= num[i] && num[i] <= &#39;z&#39;)
            result += pow(before, length - i - 1) * (num[i] - 87);
        else if(&#39;0&#39; <= num[i] && num[i] <= &#39;9&#39;)
            result += pow(before, length - i - 1) * (num[i] - 48);
    }
    return (int)result;
}
char * Ten_MoreThanTen(int after, int num)   //10转换10以上
{
    int i = 0;
    int j = 0;
    int tmp = 0;    //存储每次余数的中间变量
    char tmp_array[N] = "\0";   //转换后未倒置的数组
    for(i = 0; num > 0; i++)
    {
        tmp = num % after;
        if(tmp < 10)   //对大于等于10的余数进行字母转换
            tmp_array[i] = tmp + &#39;0&#39;;
        else
            tmp_array[i] = tmp + &#39;A&#39; - 10;
        num /= after;
    }
    for(j = 0; i > 0; i--, j++)  //倒置
    {
        array[j] = tmp_array[i - 1];
        array[j + 1] = &#39;\0&#39;;
    }
    return array;   //输出转换后存储数据的字符串地址
}
int main()
{
    int before = 0;     //转换前的进制数
    int after = 0;      //转换后的进制数
    int num1 = 0;       //要转换的十进制以内的数
    char array_num1[N] = "\0"; //要转换的十进制以上的数
    int num2 = 0;       //转换之后的数
    char *str_num2;     //转换之后的数的地址
    int tmp_num1 = 0;   //判断输入是否合法时代替num1的中间变量
    int i = 0;
    int m = 0;  //计数器
    while(1)    //整个while语句用于录入以及判断输入是否合法
    {
        printf("初始进制:");
        scanf("%d", &before);
        printf("目标进制:");
        scanf("%d", &after);
        printf("初始数值:");
        if(before > 10) //通过对初始进制判断,决定
            scanf("%s", array_num1);
        else
            scanf("%d", &num1);
        for(i = 0, tmp_num1 = num1; tmp_num1 != 0; i++)
        {
            if((tmp_num1 % 10) <= before && tmp_num1 % 10 >= 0 && tmp_num1 % 10 <= 9)
                m++;
            tmp_num1 /= 10;
        }
        if(m == i)  //判断输入的数据每一位是否都小于等于进制数
            break;
        else
        {
            m = 0;  //对计数器m重新初始化
            fflush(stdin);  //清空缓存区
            printf("输入有误!请重新输入:\n");
        }
    }
    //将进制转换的四种情况分别表示
    if(before <= 10 && after <= 10)
    {
        num2 = Ten_LessThanTen(after, LessThanTen_Ten(before, num1));
        printf("\n%d进制的%d对应的%d进制数为:%d\n", before, num1, after, num2);
    }
    else if(before > 10 && after <= 10)
    {
        num2 = Ten_LessThanTen(after, MoreThanTen_Ten(before, array_num1));
        printf("\n%d进制的%s对应的%d进制数为:%d\n", before, array_num1, after, num2);
    }
    else if(before <= 10 && after > 10)
    {
        str_num2 = Ten_MoreThanTen(after, LessThanTen_Ten(before, num1));
        printf("\n%d进制的%d对应的%d进制数为:%s\n", before, num1, after, str_num2);
    }
    else if(before > 10 && after > 10)
    {
        str_num2 = Ten_MoreThanTen(after, MoreThanTen_Ten(before, array_num1));
        printf("\n%d进制的%s对应的%d进制数为:%s\n", before, array_num1, after, str_num2);
    }
    system("pause");
    
    return 0;
}

运行结果:

How to achieve conversion between arbitrary bases in C language

总结:以上就是本篇文章的全部内容,希望对大家有所帮助。

The above is the detailed content of How to achieve conversion between arbitrary bases in C language. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn