将位数组转换为十六进制数

时间:2012-02-12 20:31:01

标签: c

从表示数字位的整数数组转换为十六进制的最佳方法是什么?

我当前尝试一次读取四位然后尝试打印相应的十六进制字符,但我看不到任何输出。

这就是我所拥有的:

 /**
  * Maps a 4 bit string in binary to the corresponding
  * hex digit
  */
 void map_bin_to_hex(char *c)
   {
      printf("In mapbin: C = %s\n", c); //This is printing funny output
      if(!strcmp(c, "0000"))      printf("0");
      else if(!strcmp(c, "0001")) printf("1");
      else if(!strcmp(c, "0010")) printf("2");
      else if(!strcmp(c, "0011")) printf("3");
      else if(!strcmp(c, "0100")) printf("4");
      else if(!strcmp(c, "0101")) printf("5");
      else if(!strcmp(c, "0110")) printf("6");
      else if(!strcmp(c, "0111")) printf("7");
      else if(!strcmp(c, "1000")) printf("8");
      else if(!strcmp(c, "1001")) printf("9");
      else if(!strcmp(c, "1010")) printf("A");
      else if(!strcmp(c, "1011")) printf("B");
      else if(!strcmp(c, "1100")) printf("C");
      else if(!strcmp(c, "1101")) printf("D");
      else if(!strcmp(c, "1110")) printf("E");
      else if(!strcmp(c, "1111")) printf("F");
   }

 /**
  * Reads 4 array elements at a time, passing them to map_bin_to_hex
  */
 void bin_array_to_hex(int *b, int length)
 {
    int i, j, k;

    //read 4 bits 16 times
      for(i = 0; i < 64; i+=4)
      {
         char hexB[4];
         for(k = 0, j = i; j < (i+4); j++, k++)
         {
            hexB[k] = b[j];
            printf("h[%d] = %d\n", k, hexB[k]);
         }
         map_bin_to_hex(hexB);
      }
      printf("\n");
  }

 int main()
 {
   // a 64-bit number represented as an array of bits
   int x[] =[1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1,1,1,1,1,1,1,1,
             1,1,1,1,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,0,0,0,0,1,0,1,0,0,0,1,0];
   bin_array_to_hex(x, 64);
  }

bin_array_to_hex()函数读取4bit节中的数组,然后将它们传递给map_bin_to_hex(),bin_array_to_hex()的输出是正确的,但我的映射函数有问题。

任何帮助都将不胜感激。

编辑:新解决方案

   void map_bin_to_hex(char *c)
   {
        char hexMap[] = 
        {'0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F'};

        int index = strtoull(c, NULL, 2);
        printf("%c", hexMap[index]);
   }

3 个答案:

答案 0 :(得分:3)

虽然您的程序将使用dasblinkenlight's answer运行,但如果您不使用strcmp,它可以运行得更快。首先,你可以自己编写一个打印字节十六进制的函数:

void printHexChar(unsigned char value){
    unsigned char lower = value & 0xf; // Use binary 'and' to mask the lower byte
    unsigned char upper = (value & 0xf0) >> 4; // Use binary 'and' to mask upper byte

    if(lower >= 10) // If lower is in range [10-15], than add a value [0-5] on 'a'.
        lower = 'a' + (lower - 10);
    else
        lower = lower + '0'; // It's in range [0-9], so we have to add it to '0'.

    if(upper >= 10) // Same as lower
        upper = 'a' + (upper - 10);
    else
        upper = upper + '0';

    printf("%c%c",upper,lower); // Print out the hexadecimal number
}

这将使您能够以十六进制格式打印uint8_t(长度为8位的无符号整数)。如果您不知道>>运算符:它会将所有位移位n次。例如23 >> 2 = 5,因为23 = [10111],[10111]&gt;&gt; 2 = [101] = 5.运算符<<将执行相反的操作并将位向左移位。

void bin_array_to_hex(int *b, int length){
    if(length % 8){
        printf("Must be dividable by eight!\n");
        return;
    }
    unsigned int i;
    unsigned int j;
    for(i = 0; i < length; i = j){
        unsigned char a = 0; // Has length of 8 bits
        for(j = i; j < i+8; ++j){ // take 8 bits...
            a |= b[j]<<(j-i); // and set them in a
        }
        printHexChar(a);
    }           
}

 int main(){
   // a 64-bit number represented as an array of bits
   int x[] ={1,1,0,0,1,1,0,0,0,0,0,0,0,0,0,0,1,1,0,0,1,1,0,0,1,1,1,1,1,1,1,1,
             1,1,1,1,0,0,0,0,1,0,1,0,1,0,1,0,1,1,1,1,0,0,0,0,1,0,1,0,0,0,1,0};
   bin_array_to_hex(x, 64);
  }

答案 1 :(得分:2)

你快到了,

替换

hexB[k] = b[j];

hexB[k] = b[j] + '0';

此外,char hexB[4];必须为char hexB[5];(最后一个字符用于空终止;在调用hexB[4]='\0';之前不要忘记设置map_bin_to_hex。)

完成此初步修复后,请查看互联网上提供的其他解决方案:您将看到如何以五行或六行完成整个操作。

编辑作为额外的挑战,看看你是否可以使用这两个字符串常量来避免第一种方法中if-then-else语句的长链:

"0000000100100011010001010110011110001001101010111100110111101111"
"0123456789abcdef"

提示:观察第一个字符串的长度恰好是第二个字符串的四倍。

答案 2 :(得分:0)

对于Zeta的答案,= 10的情况应该包含在第一个条件本身中。因此,修改后的代码看起来像

if(lower> = 10)