十六进制和二进制的转换

#include <stdio.h>

#include <math.h>

#include <string.h>

char* strrev(char* s)

void binary_hex(int n, char hex[]);

int hex_binary(char hex[]);


char* strrev(char* s)

{

    /* h指向s的头部 */

    char* h = s;

    char* t = s;

    char ch;

    /* t指向s的尾部 */

    while(*t++){};

    t--;    /* t++抵消 */

    t--;    /* 回跳过结束符'\0' */

    /* ht未重合时,交换它们所指向的字符 */

    while(h < t)

    {

        ch = *h;

        *h++ = *t;    /* h向尾部移动 */

        *t-- = ch;    /* t向头部移动 */

    }

    return(s);

}


int main()

{

    char hex[20],c;

    int n;

    printf("Instructions:\n");

    printf("Enter h to convert binary to hexadecimal:\n");

    printf("Enter b to hexadecimal number to binary:\n");

    printf("Enter a character: ");

    scanf("%c",&c);

    if (c=='h' || c=='H')

    {

        printf("Enter binary number: ");

        scanf("%d",&n);

        binary_hex(n,hex);

        printf("Hexadecimal number: %s",hex);

    }

    if (c=='b' || c=='B')

    {

        printf("Enter hexadecimal number: ");

        scanf("%s",hex);

        printf("Binary number: %d",hex_binary(hex));

    }

    return 0;

}


void binary_hex(int n, char hex[]) /* Function to convert binary to h. */

{

    int i=0,decimal=0, rem;

    while (n!=0)

    {

        decimal += (n%10)*pow(2,i);

        n/=10;

        ++i;

    }

/* At this point, variable decimal contains binary number in decimal format. */

    i=0;

    while (decimal!=0)

    {

        rem=decimal%16;

        switch(rem)

        {

            case 10:

hex[i]='A';

break;

            case 11:

hex[i]='B';

break;

            case 12:

hex[i]='C';

break;

            case 13:

hex[i]='D';

break;

            case 14:

hex[i]='E';

break;

            case 15:

hex[i]='F';

break;

            default:

hex[i]=rem+'0';

break;

        }

        ++i;

        decimal/=16;

    }

    hex[i]='\0';

    strrev(hex);       /* Function to reverse string. */

}


int hex_binary(char hex[])   /* Function to convert hexadecimal to binary. */

{

    int i, length, decimal=0, binary=0;

    for(length=0; hex[length]!='\0'; ++length);

    for(i=0; hex[i]!='\0'; ++i, --length)

    {

        if(hex[i]>='0' && hex[i]<='9')

            decimal+=(hex[i]-'0')*pow(16,length-1);

        if(hex[i]>='A' && hex[i]<='F')

            decimal+=(hex[i]-55)*pow(16,length-1);

        if(hex[i]>='a' && hex[i]<='f')

            decimal+=(hex[i]-87)*pow(16,length-1);

    }

/* At this point, variable decimal contains the hexadecimal number in decimal format. */

    i=1;

    while (decimal!=0)

    {

        binary+=(decimal%2)*i;

        decimal/=2;

        i*=10;

    }

    return binary;

}

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值