English 中文(简体)
C++ 将双轨迹转换为精细体
原标题:C++ converting binary string to decimal string

我的双轨迹可能超过64个轨道/组合。 我想将其改为10基数,以便我能够将其产出到ole。

据我所知,C++没有支持任何超过64倍的分类账,但编辑特定类型除外。 因此,如果我希望以人类可读的形式将其产出到奥洛尔,那么我就需要将双轨座改成基10体(而不是基数10英寸)。

我有以下法典......


int char_to_int(const char c) {
    return ((int) c) - 48;
}

std::string string_addition(std::string s1, std::string s2) {
    std::string ret = "";
    // making sure the two strings are the same length
    while(s1.size() > s2.size())
        s2 =  0  + s2;
    while (s2.size() > s1.size())
        s1 =  0  + s1;
    
    // adding and carrying
    for (int32_t i = (int32_t) s1.size(); i >= 0; i--)
        ret[i] = (char) ((char_to_int(s1[i]) + char_to_int(s2[i])) + 48);
    for (int32_t i = (int32_t) ret.size() - 1; i >= 0; i--)
        ...
    // then finally returning
    return ret
}

std::string to_base_ten(const std::string& s) {
    std::string ret = "";
    for (size_t i = 0; i < s.size(); i++) {
        // for each digit, calculate the appropriate number to add
        int64_t temp = s[i] * (int) std::pow(2, i);
        // performing addition with strings because of potential integer overflow issues
        ret = string_addition(ret, std::to_string(temp));
    }

    return ret;
}

...which just gets tedious and hard to read/understand because of converting with strings. Are there any simpler or more efficient ways of accomplishing this task?

问题回答

Are there any simpler or more efficient ways of accomplishing this task?

我将将此列为对“把双轨扼制到恶性扼杀”的一般问题的答复。

假设这个问题必须涉及任意大小,那么一种解决办法是使用任意的生态图书馆。 其中一个图书馆是:rel=“nofollow noretinger” 类型。

下面的例子说明使用<代码>cpp_int,以及std:accumulate功能,根据处理中的现有双位数,进行精细微调:

#include <boost/multiprecision/cpp_int.hpp>
#include <iostream>
#include <string>
#include <numeric>

namespace mp = boost::multiprecision;

std::string to_base_10(std::string binary_digits)
{
    using Int = mp::cpp_int;
    int curPower = 0;
    return std::accumulate(binary_digits.rbegin(), binary_digits.rend(), Int(),
                    [&](Int total, char ch) 
                    { 
                        if ( ch ==  1 )  // check if digit is 1
                            total += mp::pow(mp::cpp_int(2), curPower); // equivalent to total + 2^curPower                   
                        ++curPower;  // add 1 to the current power 
                        return total;                            
                    }).str(); // convert the final result to a string
}

int main() 
{
    auto s = to_base_10("10001110000011010101010101111000001101010100111000111000111110001110001111111000000011111111110");
    std::cout << s;
}

产出:

21981495524443696438500395006

<代码>std:accumulate function will process the string from right-to-left (useing the my iterators rbeginand rend, eck to see the present list is 1.

If the digit is 1, then then value of 2^current_power is added to the total. The current power is incremented, and the process is repeated. The str() function is a member of cpp_int that converts the final result to a string.

还注意到没有<代码>显示_addition 功能。 在建立<代码>cpp_int时,并不必要,最终将在处理结束时改写。

我也解决了类似的问题,涉及多重复而不是增加,使这个问题更加可读,而不是增加“可人读的扼杀”,而你可以尝试增加“可由计算机阅读的扼杀”,因为我指的是,代表0的特性应当仅仅是0,而不是人类可读的48,这样就避免了不得不将char变成暗中和背后多次,这样你才能做这样的事情。

// adding and carrying
for (int32_t i = (int32_t) s1.size(); i >= 0; i--)
    ret[i] = s1[i] + s2[i];
for (int32_t i = (int32_t) ret.size() - 1; i >= 0; i--)

When having to output the string for humans to read, just make a function that iterates through the string and outputs each character corresponding to the "computer readable string" (you may not need the cast to a char)...

void output_number_string(const std::string& s) {
    for (auto l : s)
        std::cout << ((char) (l + 48));
}

关于“std:pow(2, i)”,它也容易过度流入,以避免你在执行任务时使用无超支的职能。

std::string to_base_ten(const std::string& s) {
    std::string ret = "";
    //initialize two_power to be a string of length one containing only the "computer readable one"
    std::string two_power = string(1, 1);
    for (size_t i = 0; i < s.size(); i++) {
        //in binary, you only need to add a power of two when the bit isn t 0
        if (s[i] == 0) continue;
        // performing addition with strings because of potential integer overflow issues
        ret = string_addition(ret, two_power);
        two_power = string_addition(two_power, two_power);
    }
return ret;
}

one extra thing I may have noticed is that the program can t handle the case when the result string is greater than the original strings, but you can just simply insert a 0 at the start of the string after making sure they re the same size:

// making sure the two strings are the same length
ret =  0  + ret;
// ...




相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签