簡體   English   中英

如果二進制數等於十六進制字母,則將二進制數轉換為十六進制數的 C 程序不能轉換兩個以上的二進制數

[英]C Program to convert binary numbers to Hex can't convert more than two binary numbers if they equal hex letters

例如,如果我輸入 101010101010 它應該等於 AAA 但它不起作用。 如果一次少於兩個字母,它只會返回字母。 例如,如果我輸入“10101010”,它就可以工作。 (它返回 AA)。 請幫忙?

#include <stdio.h>
#include <math.h>
#include <string.h>
void binary_hex(int n, char hex[]);
int hex_binary(char hex[]);

int main()
{
    printf("Enter binary number: ");
    scanf("%d",&n);
    binary_hex(n,hex);
    printf("Hexadecimal number: %s",hex);
}

void binary_hex(int n, char hex[]) /* Function to convert binary to hexadecimal. */
{
    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. */
}

您尚未展示完整的程序 - n未在main任何范圍內聲明。 假設int n ,你的第一個問題在這里:

printf("Enter binary number: ");
scanf("%d",&n);

您正在以 10(十進制)整數形式讀取 1 和 0 的字符串。 相反,您應該編寫一個函數,它接受一個const char *並迭代每個字符,驗證它是 1 還是 0,解釋每個值,並累加總數。 提示:按從右到左的順序工作可能更容易。

int parse_binary(const char *str)
{
    int total = 0;

    while (...) {
       total += ...
    }

    return total;
}

另一個提示:不要重新發明輪子。 當然,在您之前有人想用 C 解析二進制數,不是嗎? 查看strtoul - 即其第三個參數的描述。

注意:到目前為止,我提到的兩件事都將您的二進制值限制為 32 位(或 64 位,取決於您選擇的數據類型)。 如果您不需要實際使用整數值,任何只將 1 和 0 字符串轉換為十六進制值字符串(任意長度),那么您可以將問題分解為更簡單的步驟。

我們真正喜歡使用十六進制的原因是十六進制值中的每個字符正好代表一個半字節,也就是四位。 這意味着每組四個 1 或 0 正好對應一個十六進制字符。 假設您的輸入總是長度為 4 個字符的倍數,您可以輕松處理任意長的字符串。

D    E    A    D    B    E    E    F
1101 1110 1010 1101 1011 1110 1110 1111

一個非常適合初學者的解決方案。 這將適用於大二進制數/字符串。

#include <stdio.h>
#include <string.h>
int main()
{
    int hexConstant[] = {0, 1, 10, 11, 100, 101, 110, 111, 1000, 1001, 1010, 1011, 1100, 1101, 1110, 1111};

    char hex[20000];
    char binary[20000];
    int index, i, digit;

    printf("Enter binary string: ");
    scanf("%s", binary);

    index = 0;
    int len = strlen(binary) ;
    for(int j =  len-1; j>=0; j-=4) {

         if(j-3>=0) digit = (binary[j-3]-'0')*1000 + (binary[j-2]-'0')*100 + (binary[j-1]-'0') * 10 + (binary[j]-'0');
         else if(j-3>=0) digit = (binary[j-2]-'0')*100 + (binary[j-1]-'0') * 10 + (binary[j]-'0');
         else if(j-1>=0) digit = (binary[j-1]-'0') * 10 + (binary[j]-'0');
         else digit = (binary[j]-'0');

         for(i=0; i<16; i++)
        {
            if(hexConstant[i] == digit)
            {
                if(i<10)
                {
                    hex[index] = (char)(i + 48);
                }
                else
                {
                    hex[index] = (char)((i-10) + 65);
                }

                index++;
                break;
            }
        }
    }

    hex[index] = '\0';

    strrev(hex);

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

    return 0;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM