新客网WWW.XKER.COM:致力做中国最专业的网络学院!
学院: 操作系统 - 网络应用 - 服务器 - 网络安全 - 工具软件 - 办公软件 - Web开发 - 数据库 - 网页设计 - 图形图像 - 媒体动画 - 硬件学堂 - 存储频道 - QQ专区
您的位置:首页 > 网络学院 > 网络应用 > 网管知识 > 正文:二到十六进制之间的相互转换代码详细解析

二到十六进制之间的相互转换代码详细解析

新客网 XKER.COM 2007-07-31 来源: sad 收藏本文

  此程序是用作二到十六进制的相互转换,用法如下:
  gcc advanceChange.c -o advanceChange
  ./advanceChange 1111 2 10
  意思是把二进制1111转换为十进制,结果是15

  源代码:

#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<malloc.h>

#define oops() {perror("malloc");exit(1);}

typedef struct ooput{
    int num;
    struct ooput *pre,*next;
}out;

//乘方运算,返回a的b次方的值

int power(int a,int b){
    int i;
    int x = a;
    if (b == 0)
        return 1;
    else if (b == 1)
        return a;
    else{
        for (i=1;i<b;i++){
            a = a * x;
        }
        return a;
    }
}

int translate(int time,char c[],int advance){
    int i,ten = 0;    //变量ten是用来得到转换成十进制后的值

    int arg;    //把字符转换成整型后赋给此变量


    for (i=0;i<time;i++){
        //为字符时如何,为数字时如何

        if (isalpha(c[time-i-1])){
            //按标准,进制中的字母应该是大写

            if (isupper(c[time-i-1]))
                arg = c[time-i-1] - 55;
            else{
                printf("请用大写字母\n");
                exit(1);
            }
        }
        else
            arg = c[time-i-1] - 48;
        if (arg > (advance-1)){
            printf("书写错误:%s 不是 %d进制\n",c,advance);
            exit(1);
        }
        //得到转换成十进制的数值并将其赋给变量ten

        ten = ten + (arg * power(advance,i));
    }
    return ten;
}

//返回n进制转换后的十进制值

int first(char *parament1,char *parament2){
    int replay;

    //得到n进制的长度,然后将其转换成char型数组,这样才能将其一个一个的进行转换

    int time = strlen(parament1);    
    char c[time];
    sprintf(c,"%s",parament1);

    //根据对应的进制数,然后将其转换成十进制

    if (strcmp(parament2,"2") == 0)
        replay = translate(time,c,2);
    else if (strcmp(parament2,"3") == 0)
        replay = translate(time,c,3);
    else if (strcmp(parament2,"4") == 0)
        replay = translate(time,c,4);
    else if (strcmp(parament2,"5") == 0)
        replay = translate(time,c,5);
    else if (strcmp(parament2,"6") == 0)
        replay = translate(time,c,6);
    else if (strcmp(parament2,"7") == 0)
        replay = translate(time,c,7);
    else if (strcmp(parament2,"8") == 0)
        replay = translate(time,c,8);
    else if (strcmp(parament2,"9") == 0)
        replay = translate(time,c,9);
    else if (strcmp(parament2,"10") == 0)
        replay = translate(time,c,10);
    else if (strcmp(parament2,"11") == 0)
        replay = translate(time,c,11);
    else if (strcmp(parament2,"12") == 0)
        replay = translate(time,c,12);
    else if (strcmp(parament2,"13") == 0)
        replay = translate(time,c,13);
    else if (strcmp(parament2,"14") == 0)
        replay = translate(time,c,14);
    else if (strcmp(parament2,"15") == 0)
        replay = translate(time,c,15);
    else if (strcmp(parament2,"16") == 0)
        replay = translate(time,c,16);

    return replay;
}

//用一个双向链表储存转换后的值,最后返回链表的最后一个结点

out *output(int total,int advance){
    //k在这个程序中并无实际意义,这的作用就是为了让程序区分中链表的第一个结点和其它的结点

    int k = 1;
    out *put,*pf;;

    while (total){
        if ((put=(out *)malloc(sizeof(out))) == NULL)
            oops();
        //将取得的模赋给ooput结构中的num变量

        put->num = total % advance;
        //将值赋给链表中第一个结点时如何,除第一个结点外其它的结点如何

        if (k == 1){
            put->next = NULL;
            put->pre = NULL;
            //只所以把put赋给两个指针变量是为了能返回链表的第一个结点

            pf = put;
        }else{
            pf->next = put;
            put->pre = pf;
            put->next = NULL;
            pf = put;
        }
        total /= advance;
        k++;
    }

    return pf;
}

//将两个n进制相加后的值转换成对应的n进制

out *second(int total,char *parament){
    out *head;

    if (strcmp(parament,"2") == 0)
        head = output(total,2);
    else if (strcmp(parament,"3") == 0)
        head = output(total,3);
    else if (strcmp(parament,"4") == 0)
        head = output(total,4);
    else if (strcmp(parament,"5") == 0)
        head = output(total,5);
    else if (strcmp(parament,"6") == 0)
        head = output(total,6);
    else if (strcmp(parament,"7") == 0)
        head = output(total,7);
    else if (strcmp(parament,"8") == 0)
        head = output(total,8);
    else if (strcmp(parament,"9") == 0)
        head = output(total,9);
    else if (strcmp(parament,"10") == 0)
        head = output(total,10);
    else if (strcmp(parament,"11") == 0)
        head = output(total,11);
    else if (strcmp(parament,"12") == 0)
        head = output(total,12);
    else if (strcmp(parament,"13") == 0)
        head = output(total,13);
    else if (strcmp(parament,"14") == 0)
        head = output(total,14);
    else if (strcmp(parament,"15") == 0)
        head = output(total,15);
    else if (strcmp(parament,"16") == 0)
        head = output(total,16);

    return head;;
}

//得到链表的最后一个结点,从后向前输出链表内的值

void print(out *head){
    while (head != NULL){
        //当数值在10-15之间时将其转换成字符并打印

        if (head->num > 9){
            printf("%c",head->num + 55);
        }else
            printf("%d",head->num);
        head = head->pre;
    }
    printf("\n");
}

int main(int argc,char *argv[]){
    int ten;
    out *head;

    if (argc != 4){
        printf("错误:参数太少或太多\n");
        exit(1);
    }
    ten = first(argv[1],argv[2]);
    head = second(ten,argv[3]);
    print(head);
    return 0;
}

收藏】 【评论】 【推荐】 【投稿】 【打印】 【关闭
发表评论
要记得去论坛讨论,点击注册新会员匿名评论
评论内容:不能超过250字,需审核后才会公布,请自觉遵守互联网相关政策法规。
阅读排行
随机推荐
实用信息推荐