【问题标题】:Instead of printing the binary number out how would I store it as a variable?我将如何将其存储为变量,而不是将二进制数打印出来?
【发布时间】:2017-03-31 12:25:08
【问题描述】:

我将一个十六进制数传入 hex2bin,它会正确打印出二进制数,但我不希望它打印出我想要返回的数字,因此我可以使用它来查找数字的基数。我将如何存储号码而不是打印出来?

int hex2bin (int n){
    int i,k,mask;
    for(i = sizeof(int) * 8 - 1; i >= 0; i--){
         mask = 1 << i;
         k = n & mask;
         k == 0 ? printf("0"):printf("1");
    }
    return 0;
}

【问题讨论】:

  • 你传入的数字'n'和它的二进制表示是相同的,因为它们都以0和1的形式存储在内存中。如果我理解你的问题,那么你不需要传递给hex2bin,你只需要分析基数而不是以你选择的任何方式传递给printf。该方法本质上是相同的,但您必须注意您的位顺序(它与打印顺序相反)

标签: c binary hex


【解决方案1】:

也许是这样的?

int result = 0;
int i, k...
...
result = result | (((k == 0) ? 0 : 1) << i;
...
return result;

您当然也可以简单地使用变量数组,而不是巧妙地使用 int。

【讨论】:

    【解决方案2】:

    将数字存储在一个字符串中,其空格由复合文字提供(自 C99 起可用)。

    它的工作原理类似于 OP 的流程:循环到 sizeof(int) * 8 次,找到 1 位的值并打印/保存。

    #include <stdio.h>
    #include <stdlib.h>
    #include <limits.h>
    
    // Maximum buffer size needed
    #define UTOA_BASE_2 (sizeof(unsigned)*CHAR_BIT + 1)
    
    char *utoa_base2(char *s, unsigned x) {
      s += UTOA_BASE_2 - 1;
      *s = '\0';
      do {
        *(--s) = "01"[x % 2];
        x /= 2;
      } while (x);
      return s;
    }
    
    #define TO_BASE2(x) utoa_base2((char [UTOA_BASE_2]){0} , (x))
    
    void test(unsigned x) {
      printf("base10:%10u base2:%5s ", x, TO_BASE2(x));
      char *s = TO_BASE2(x);
      // do stuff with `s`, it is valid for until the end of this block
      printf("%s\n", s);
    }
    
    int main(void) {
      test(0);
      test(25);
      test(UINT_MAX);
    }
    

    样本输出

    base10:         0 base2:    0 0
    base10:        25 base2:11001 11001
    base10:4294967295 base2:11111111111111111111111111111111 11111111111111111111111111111111
    

    这是base-n answer 的变体。

    【讨论】:

      【解决方案3】:

      您可以使用strcat 函数来执行此操作。

      请注意,此答案中的新 hex2bin 函数假定参数 char *buf 已被分配,并且至少可以保存 1+sizeof(int)*8 字节,包括空终止符:

      #include <stdio.h>
      #include <stdlib.h>
      #include <string.h>
      
      // assume: buf is at least length 33
      int hex2bin (int n, char *buf)
      {
          int i,k,mask;
          for(i = sizeof(int) * 8 - 1; i >= 0; i--){
               mask = 1 << i;
               k = n & mask;
               k == 0 ? strcat(buf, "0") : strcat(buf, "1");
          }
          return 0;
      }
      
      int main()
      {
          int n = 66555;
          char buffer[1+sizeof(int)*8] = { 0 } ;
          hex2bin(n, buffer);
          printf("%s\n", buffer);
          return 0;
      }
      

      【讨论】:

        【解决方案4】:

        希望对您有所帮助:)

        bool convertDecimalBNR(INT32 nDecimalValue, UINT32 * punFieldValue, INT32 nBitCount, DecimalBNRType * pDecimalSpecification)
        {
          bool   bBNRConverted = false;
          INT32  nBitIndex = nBitCount - 1;
          INT32  nBitValue = anTwoExponents[nBitIndex];
        
          *punFieldValue = 0;
        
          if ((nDecimalValue >= pDecimalSpecification->nMinValue) && (nDecimalValue <= pDecimalSpecification->nMaxValue))
          {
                // if the value is negative, then add (-1 * (2 ^ (nBitCount - 1))) on itself and go on just like a positive value calculation.
            if (nDecimalValue < 0)
            {
              nDecimalValue += nBitValue;
              nBitIndex--;
              nBitValue /= 2;
              *punFieldValue |= BIT_0_ONLY_ONE;
            }
        
            while (nBitIndex >= 0)
            {
              *punFieldValue = (*punFieldValue << 1);
        
              if (nDecimalValue >= nBitValue)
              {
                nDecimalValue -= nBitValue;
                *punFieldValue |= BIT_0_ONLY_ONE;
              }
        
              nBitIndex--;
              nBitValue /= 2;
            }
        
            if (nDecimalValue <= nBitValue)
            {
              bBNRConverted = true;
            }
          }
        
          return (bBNRConverted);
        }
        

        【讨论】:

        • 如何为 base64 更改这个?
        • @ShaolinGOD base64是什么意思?如果您要求 64 位,那么您应该使用“typedef long INT64;”而不是使用“typedef int INT32;”我想。
        猜你喜欢
        • 1970-01-01
        • 2022-11-29
        • 2022-08-10
        • 2021-09-06
        • 1970-01-01
        • 2012-02-23
        • 1970-01-01
        • 2019-07-02
        • 1970-01-01
        相关资源
        最近更新 更多