簡體   English   中英

如何在 C 中將十六進制轉換為二進制並將二進制輸出為 char 數組?

[英]How to convert Hex to binary in C and output binary as a char array?

我正在嘗試編寫一個將十六進制字符轉換為二進制數組的函數,因此很容易一一讀取所有的一和零。 我的代碼有問題。 我目前已經編寫了下面的代碼。 它給了我一個錯誤,說它返回一個局部變量的地址,即使我試圖讓它返回一個char

char *hex_to_bin(const char input[]) {
    char output[] = "";
    int i = 0;
    while (input[i]) {
        switch (input[i]) {
          case '0':
            strcat(output, "0000");
            break;
          case '1':
            strcat(output, "0001");
            break;
          case '2':
            strcat(output, "0010");
            break;
          case '3':
            strcat(output, "0011");
            break;
          case '4':
            strcat(output, "0100");
            break;
          case '5':
            strcat(output, "0101");
            break;
          case '6':
            strcat(output, "0110");
            break;
          case '7':
            strcat(output, "0111");
            break;
          case '8':
            strcat(output, "1000");
            break;
          case '9':
            strcat(output, "1001");
            break;
          case 'A':
            strcat(output, "1010");
            break;
          case 'B':
            strcat(output, "1011");
            break;
          case 'C':
            strcat(output, "1100");
            break;
          case 'D':
            strcat(output, "1101");
            break;
          case 'E':
            strcat(output, "1110");
            break;
          case 'F':
            strcat(output, "1111");
            break;
          case 'a':
            strcat(output, "1010");
            break;
          case 'b':
            strcat(output, "1011");
            break;
          case 'c':
            strcat(output, "1100");
            break;
          case 'd':
            strcat(output, "1101");
            break;
          case 'e':
            strcat(output, "1110");
            break;
          case 'f':
            strcat(output, "1111");
            break;
        }
        i++;
    }
    return output;
}

也許這是一種愚蠢的方法,但我最近才開始使用 C。

您的代碼中有兩個主要問題:

  • char output[] = ""; 定義一個足夠大的char數組來容納空字符串。 出於您的目的,您應該分配一個大小為4 * strlen(input) + 1的數組
  • return output; 返回本地對象的地址,一旦函數返回,該地址將變為無效。 您可以改為使用malloc()從堆中分配內存並返回指針。 調用者將負責釋放該對象。

這是修改后的版本:

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

char *hex_to_bin(const char input[]) {
    char *output = malloc(strlen(input) * 4 + 1);
    if (output != NULL) {
        char *p = output;
        *p = '\0';
        for (size_t i = 0; input[i]; i++) {
            switch (input[i]) {
              case '0':
                strcpy(p, "0000");
                break;
              case '1':
                strcpy(p, "0001");
                break;
              case '2':
                strcpy(p, "0010");
                break;
              case '3':
                strcpy(p, "0011");
                break;
              case '4':
                strcpy(p, "0100");
                break;
              case '5':
                strcpy(p, "0101");
                break;
              case '6':
                strcpy(p, "0110");
                break;
              case '7':
                strcpy(p, "0111");
                break;
              case '8':
                strcpy(p, "1000");
                break;
              case '9':
                strcpy(p, "1001");
                break;
              case 'A':
              case 'a':
                strcpy(p, "1010");
                break;
              case 'B':
              case 'b':
                strcpy(p, "1011");
                break;
              case 'C':
              case 'c':
                strcpy(p, "1100");
                break;
              case 'D':
              case 'd':
                strcpy(p, "1101");
                break;
              case 'E':
              case 'e':
                strcpy(p, "1110");
                break;
              case 'F':
              case 'f':
                strcpy(p, "1111");
                break;
              default:
                p[0] = input[i];
                p[1] = '\0';
                break;
            }
            p += strlen(p);
        }
    }
    return output;
}

int main() {
    char *p = hex_to_bin("n = DeadBeef\n");
    printf("%s\n", p);
    free(p);
    return 0;
}

代碼失敗,因為它試圖返回一個過小的本地數組。 output[]太小,函數結束后局部數組無效。

相反,請考慮傳入一個目標數組。

// Return NULL on error
char *hex_to_bin(size_t size, char *dest, const char input[]) {
  // Enough room? 
  size_t len = strlen(input);
  if (dest == NULL || len * 4 + 1 < size) {
    return NULL;
  }

  char *s = dest;
  size_t i;
  // Iterate until a non-hex digit found ....
  for (i = 0; isxdigit((unsigned char) input[i]); i++) {
    // Rather than a huge switch table, how about scanning a string of length 1?
    unsigned digit;
    sscanf((char[2]){ input[i], '\0'}, "%x", &digit);
    // Now convert to binary 
    for (unsigned mask = 8; mask; mask >>= 1) {
      *s++ = mark & digit ? '1' : '0';
    }
  }
  *s = '\0';
  if (input[i] != '\0') { // Was a non-hex digit found?
    return NULL; 
  }
  return dest;
}                      

暫無
暫無

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

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