十六进制转化二进制[c]

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int hex2dec(char c)
{
    int i=0;
    //char cc[1]={0};
    //cc[0]=c;
    if(c>='0'&&c<='9')  
        i=c-'0';       //('0')48-48=0
    else if(c>='A'&&c<='Z')
        i=c-'0'-7;    //(A)65-48-7=10
    else if(c>='a'&&c<='z')
        i=c-'0'-39;  //(a)97-48-39=10
    //if(cc[0]>='0'&&cc[0]<='9')  
 //       i=cc[0]-'0';       //('0')48-48=0
    //else if(cc[0]>='A'&&cc[0]<='Z')
    //    i=cc[0]-'0'-7;    //(A)65-48-7=10
    //else if(cc[0]<='a'&&cc[0]<='z')
    //    i=cc[0]-'0'-39;  //(a)97-48-39=10
    else
    {
        printf("error!\n");
        exit(0);
    }
    return i;
}
int main()
{
    int j=0;
    int index=0;
    char print[65]={0};
    char *str[16]=
    {
        "0000","0001","0010","0011",    //0-3
        "0100","0101","0110","0111",    //4-7
        "1000","1001","1010","1011",    //8-11
        "1100","1101","1110","1111"     //12-15
    };
    char hex[17];
    while (1)
    {
        scanf("%s",hex);
        for(j=strlen(hex)-1;j>=0;j--)
        {
            index=hex2dec(hex[j]);
            strcat(print,str[index]);
            strcat(print," ");
        }
        printf("%s转化结果:%s\n",hex,print);
        memset(print,0,65);
    }
    

}

posted @ 2014-04-04 14:32  乾卦  阅读(443)  评论(0编辑  收藏  举报