【问题标题】:how to convert a binary string 128bit to decimal string in c++? [closed]如何在 C++ 中将二进制字符串 128 位转换为十进制字符串? [关闭]
【发布时间】:2020-09-16 18:52:33
【问题描述】:
void convertBinaryToDecimal(std::string BinarySrc, std::string& DecimalDest)
{

}

我需要将一个二进制长度为 128 位的字符串转换为十进制字符串

【问题讨论】:

标签: c++ string algorithm binary bit-manipulation


【解决方案1】:

算法很简单:

  • 使用初始化为0的128位无符号变量v
  • 对于从BinarySrcv = v + v + bit 读取的每个位;
  • 然后产生十进制数字:digit = '0' + v % 10(v /= 10) != 0
  • 最后,反转十进制数字字符串。

这应该很容易转换为代码。

【讨论】:

    【解决方案2】:

    对于用户 chqrlie 给出的答案,我会立即问,现在我们在哪里有 128 位无符号整数变量,即使有,我们将如何处理 500 位字符串?所以,这种方法当然行不通。

    这里我们需要的是二进制数的整数除法。然后,我们将执行与小数相同的操作。将整数除以 10,余数就是我们感兴趣的数字。

    下面显示的功能适用于带有二进制数的任意长字符串。也可以使用其他数基

    请看:


    编辑

    根据用户chux

    的非常好的观察更新了
    #include <iostream>
    #include <string>
    #include <algorithm>
    
    int main() {
    
        // This is the input number
        std::string binAsString{ "101010" };
    
        // Show orignial string to user
        std::cout << "\n\n" << binAsString;
    
        // Here we will store the resulting output
        std::string result{};
    
        // The conversion will also work for other  number bases
        // For base > 10 you need to adapt the creation of the digit at the bottom
        constexpr unsigned int numberBase{ 10 };
    
        // So, we will perfrom an integer division by 10, until the number is 0
        do {
    
            // The remainder is the digit that we are interested in
            unsigned int remainder{};
            // Temporary result of integer division
            std::string dividedNumberAsString{};
    
            // Do the division
            for (const char bit : binAsString) {
    
                // Calculate the remainder
                remainder = remainder * 2 + (bit - '0');
    
                // If we have a overflow (e.g. number is bigger than 10)
                if (remainder >= numberBase) {
    
                    // Handle overflow
                    remainder -= numberBase;
                    // Add character 1 to the "devidedString"
                    dividedNumberAsString += "1";
                }
                else {
                    dividedNumberAsString += "0";
                }
            } 
            // Now "dividedNumberAsString" as string is the result of the devision by e.g. 10 in binary form
            binAsString = dividedNumberAsString;
            // The remainder is the number that we are interested in
            result.insert(0, 1, '0' + remainder);
    
            // Continue the loop with the new binary string
        } while (std::count(binAsString.begin(), binAsString.end(), '1'));
    
        // Show result
        std::cout << "  -->\n" << result << "\n\n";
        return 0;
    }
    

    【讨论】:

    • binAsString"0"result 最终为"",而不是"0"。建议while (std::count(binAsString.begin() ... { } --> do { } while (std::count(binAsString.begin()...
    • @chux-ReinstateMonica:非常感谢你的好提示。更新
    • gcc 和 clang 都支持 64 位目标上的 128 位整数,并且 OP 没有要求通用解决方案,只要求 128 位字符串。
    • @chqrlie 非常感谢您的提示
    【解决方案3】:

    我想我可以给你一个小提示,而不是直接提供解决方案。

    取出长二进制数,将其分成两半(或四等分,或其他)。

    跟踪哪些是二进制数的上限,哪些是二进制数的下限。

    计算下限和上限的真值,然后将它们相加。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2017-02-23
      • 2014-08-03
      • 1970-01-01
      • 2012-01-22
      • 1970-01-01
      • 2019-08-23
      • 1970-01-01
      相关资源
      最近更新 更多