2014-10-28 39 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

相關問題