2014-10-28 126 views
1

我试图弄清楚为什么这个代码只用2位数的十六进制数字。例如,如果输入“11”,它将输出“00010001”,但如果输入“111”,则会给我一些随机数。我想尽量让它接受用户想要的数字。将十六进制转换为二进制

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

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

int main() 
{ 
    char hex[20],c; 
    int n; 

    printf("Enter hexadecimal number: "); 
    scanf("%s",hex); 
    printf("Binary number: %d",hex_binary(hex)); 

    system("pause"); 
    return 0;} 

//Function to convert hexadecimal to binary. 

int hex_binary(char hex[]) { 

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

工作对我很好。它提供了'100010001'输出'111'输入.. – 2014-10-28 18:00:25

+0

什么“一些随机数”是你的输出? – 2014-10-28 18:06:50

+0

您正在将二进制数存储在'int'中,您应该将其存储在'unsigned int'数组中。 – user1336087 2014-10-28 18:10:28

回答

0

您需要使用一个数组来存储二进制数,作为一个int变量将不能够存储大量数,int范围(通常)从−32767+32767C data types.

例如:


#include <stdio.h> 
#include <math.h> 

int hex_binary(char hex[], int binary_number[]); 

int main() 
{ 
    char hex[20]; 
    int binary_number[100]; 
    int j,k; 

    printf("Enter hexadecimal number: "); 
    scanf("%19s",hex); 
    j = hex_binary(hex,binary_number); 

    printf("Binary number is: "); 
    for(k=j-1;k>=0;k--) 
     printf("%d",binary_number[k]); 
    printf("\n"); 

    return 0; 
} 

//Function to convert hexadecimal to binary. 

int hex_binary(char hex[], int binary_number[]) 
{ 
    int i, j=0, length, decimal=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. 
    while (decimal!=0) 
    { 
     binary_number[j++] = decimal%2; 
     decimal/=2; 
    } 
    return j; 
} 

输出

Enter hexadecimal number: 111 
Binary number is: 100010001 

Enter hexadecimal number: 1111 
Binary number is: 1000100010001 

Enter hexadecimal number: 11111 
Binary number is: 10001000100010001 

Enter hexadecimal number: 987634 
Binary number is: 100110000111011000110100 
+0

注:“long”的_minimum_范围从-2,147,483,647到2,147,483,647。 'int'的_minimum_范围是从-32,767到32,767。 – chux 2014-10-28 18:34:43

+0

哦!好吧,我明白你的意思了。它的工作现在,非常感谢你!我感谢你的帮助:) – user3047983 2014-10-28 18:35:36

+0

@chux:你绝对正确:) – user1336087 2014-10-28 18:36:22