【问题标题】:Converting integer to binary string using itoa in C/C++在 C/C++ 中使用 itoa 将整数转换为二进制字符串
【发布时间】:2012-03-30 17:40:36
【问题描述】:

我可以使用itoa() 将 long long int 转换为二进制字符串吗?

我已经看到了使用itoa 将 int 转换为二进制的各种示例。如果我使用 long long int,是否存在溢出或精度损失的风险?

编辑

感谢大家的回复。我实现了我想要做的事情。 itoa() 不够用,因为它不支持 long long int。此外,我不能在 gcc 中使用itoa(),因为它不是标准库函数。

【问题讨论】:

  • AFAIK,itoa 将整数转换为字符串.....您能否从您提到的那些示例中提供一些示例代码?
  • itoa 不是标准函数。另外,您的意思是要将整数转换为只有二进制数字的字符串吗?
  • @SayemAhmed- 这是我可以将 int 转换为二进制字符串的链接。cplusplus.com/reference/clibrary/cstdlib/itoa
  • 屏蔽掉这些位并自己构造一个二进制字符串。
  • @JoachimPileborg - 是的,没错。

标签: c++ c itoa


【解决方案1】:

你的措辞有点混乱, 通常,如果您声明 'decimal',我的意思是:'表示为十进制数字字符串的数字',而您的意思似乎是 'integer '.

对于 'binary',我的意思是:'一个以字节表示的数字 - CPU 可以直接使用'

更好的表达主题的方法是:将 64 位整数转换为二进制数字字符串。

某些系统具有_i64toa 函数。

【讨论】:

    【解决方案2】:

    您可以为此目的使用std::bitset

    template<typename T>
    inline std::string to_binary_string(const T value)
    {
        return std::bitset<sizeof(T)>(value).to_string();
    }
    
    std::cout << to_binary_string(10240);
    std::cout << to_binary_string(123LL);
    

    【讨论】:

      【解决方案3】:

      要将整数转换为仅包含二进制数字的字符串,您可以通过使用一位掩码检查整数中的每个位并将其附加到字符串来实现。

      类似这样的:

      std::string convert_to_binary_string(const unsigned long long int value,
                                           bool skip_leading_zeroes = false)
      {
          std::string str;
          bool found_first_one = false;
          const int bits = sizeof(unsigned long long) * 8;  // Number of bits in the type
      
          for (int current_bit = bits - 1; current_bit >= 0; current_bit--)
          {
              if ((value & (1ULL << current_bit)) != 0)
              {
                  if (!found_first_one)
                      found_first_one = true;
                  str += '1';
              }
              else
              {
                  if (!skip_leading_zeroes || found_first_one)
                      str += '0';
              }
          }
      
          return str;
      }
      

      编辑:

      一种更通用的方法可能是使用模板来完成:

      #include <type_traits>
      #include <cassert>
      
      template<typename T>
      std::string convert_to_binary_string(const T value, bool skip_leading_zeroes = false)
      {
          // Make sure the type is an integer
          static_assert(std::is_integral<T>::value, "Not integral type");
      
          std::string str;
          bool found_first_one = false;
          const int bits = sizeof(T) * 8;  // Number of bits in the type
      
          for (int current_bit = bits - 1; current_bit >= 0; current_bit--)
          {
              if ((value & (1ULL << current_bit)) != 0)
              {
                  if (!found_first_one)
                      found_first_one = true;
                  str += '1';
              }
              else
              {
                  if (!skip_leading_zeroes || found_first_one)
                      str += '0';
              }
          }
      
          return str;
      }
      

      注意:static_assertstd::is_integral 都是 C++11 的一部分,但至少从 4.4.5 开始在 Visual C++ 2010 和 GCC 中都受支持。

      【讨论】:

      • 可以很容易地避免前导零。跳过 0 直到我们看到第一个 1(从左到右扫描时)。
      • @AndreasMagnusson 添加了跳过前导零的参数。 :)
      • 非常感谢 Joachim Pileborg!修改了您的代码,以便我可以在 c 中使用它,它就像一个魅力! :) :)
      • 如果做C版本,你可以把current_bit = 63改成current_bit = ((sizeof value) * 8) - 1
      • 看不到模板版是怎么工作的,bits变量从何而来?
      【解决方案4】:

      是的,你可以。正如你showed yourself,itoa 可以用 base 2 调用,这意味着二进制。

      #include <stdio.h>
      #include <stdlib.h>
      
      int main()
      {
          int i;
          char str[33];
      
          i = 37; /* Just some number. */
          itoa (i, str, 2);
          printf("binary: %s\n", str);
      
          return 0;
      }
      

      另外,是的,如果您使用大于 int 的整数类型,则会出现截断,因为 itoa() 仅将普通的“int”作为值。 long long 在您的编译器上可能是 64 位,而 int 可能是 32 位,因此编译器会在转换之前将 64 位值截断为 32 位值。

      【讨论】:

        猜你喜欢
        • 2010-09-18
        • 2018-03-01
        • 2018-07-07
        • 1970-01-01
        • 2012-08-16
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多