簡體   English   中英

如何在C中將十六進制字符串轉換為二進制字符串?

[英]How to convert an hexadecimal string into a binary string in C?

我的范圍是將十六進制字符串轉換為二進制字符串。 在我制作的程序中,我只打印。 如何將每個單個十六進制字符(在 hexa 數組中)的轉換保存為 4 位(在 binarynum 數組中)?

例子

hexa[100] = "ff" -> binarynum[100] = "11111111"

這是我的代碼:

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

int main()
{
    char binarynum[100];
    long int i = 0;

    char hexa[100] = "fff";
    while (hexa[i])
    {
        switch (hexa[i])
        {
        case '0':
            printf("0000"); break;    
        case '1':
            printf("0001"); break;
        case '2':
            printf("0010"); break;
        case '3':
            printf("0011"); break;
        case '4':
            printf("0100"); break;
        case '5':
            printf("0101"); break;
        case '6':
            printf("0110"); break;
        case '7':
            printf("0111"); break;
        case '8':
            printf("1000"); break;
        case '9':
            printf("1001"); break;
        case 'A':
            printf("1010"); break;
        case 'B':
            printf("1011"); break;
        case 'C':
            printf("1100"); break;
        case 'D':
            printf("1101"); break;
        case 'E':
            printf("1110"); break;
        case 'F':
            printf("1111"); break;
        case 'a':
            printf("1010"); break;
        case 'b':
            printf("1011"); break;
        case 'c':
            printf("1100"); break;
        case 'd':
            printf("1101"); break;
        case 'e':
            printf("1110"); break;
        case 'f':
            printf("1111"); break;
        default:
            printf("\n Invalid hexa digit %c ", hexa[i]);
            return 0;
        }
        i++;
    }
    return 0;
}

這是使用查找表的示例; 它假設您要處理 32 位int ,因此結果緩沖區的大小會相應地調整。 但是,您應該能夠根據需要對其進行調整。

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

/**
 * Maps our input character onto an array index.  '0' maps to 0,
 * '1' maps to 1, 'a' maps to 10, etc.  C guarantees that decimal
 * digit encodings are sequential, so subtracting `0` from a digit
 * character should map the value correctly.  This code assumes that
 * the encodings for `a` through `f` are sequential (as they are in
 * ASCII and UTF-8 - that *may* not be a valid assumption for
 * other encodings.
 * 
 * If the input character is neither a decimal nor hexadecimal
 * digit, this will map to index 16 which holds an empty string.
 */
#define MAP(c) (isdigit(c) ? c - '0' : (isxdigit(c) ? tolower(c) - 'a' + 10 : 16 ) )

/**
 * Takes an input value off the command line.
 */
int main( int argc, char **argv )
{
  if ( argc < 2 )
  {
    fprintf( stderr, "USAGE: %s number\n", argv[0] );
    exit( EXIT_FAILURE );
  }

  /**
   * Convert the command line argument to an integer value.
   */
  int arg = atoi( argv[1] );

  /**
   * Stores the hexadecimal string representation of the input
   * value.
   */
  char buf[9] = { 0 };

  /**
   * This will hold our final binary string
   */
  char result[33] = { 0 };

  /**
   * Our lookup table
   */
  char bits[17][5] = { "0000", "0001", "0010", "0011", "0100", "0101", "0110", "0111", 
                       "1000", "1001", "1010", "1011", "1100", "1101", "1110", "1111", "" };

  sprintf( buf, "%08x", arg );

  for ( char *b = buf; *b != 0; b++ )
    strcat( result, bits[ MAP(*b) ] );

  printf( "Binary version of %10d (0x%s) = %s\n", arg, buf, result );

  return EXIT_SUCCESS;
}

還有一些輸出:

$ ./map 1
Binary version of          1 (0x00000001) = 00000000000000000000000000000001

$ ./map 1234
Binary version of       1234 (0x000004d2) = 00000000000000000000010011010010

$ ./map 12345678
Binary version of   12345678 (0x00bc614e) = 00000000101111000110000101001110

暫無
暫無

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

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