开发者

how to convert decimal to binary in c++

I have a method to convert dec to bin

QList<bool> widgetInput::decToBin(int number)
{
        int remainder;
        QList<bool> result;
        if(number <= 1) {
            result << number;
            return result;
        }

        remainder = number%2;
        decToBin(number >> 1);
        result &l开发者_运维知识库t;< remainder;


}

but unfortunately this method only holds one element in list . but when I replace the "result << number" with "cout << number" it will work. could you please help me and let me know where is my exact problem?

regards.


On each recursive step, you are creating a new QList result; which is local to that step, then inserting the remainder into it. You don't need recursion (and in general it should be avoided when iteration will do):

QList<bool> result;

while(number > 0) {
  result << number%2;
  number /=2;
}

// Edited to add: Just realized you would also have to reverse QList here.
//  Depends on it's interface.

return result;

or better yet, just use a standard container:

bitset<sizeof(int)*CHAR_BIT> bs(number);


You're very close, but you will need to make one modification:

    remainder = number%2;
    result << remainder << decToBin(number >> 1);
    return result;

I'm not sure exactly how QList works, but the above is intended to append the result of decToBin() after the remainder in the result. You may need to modify this slightly to make it work.

The result will then contain the binary representation of the number, in "reverse" order (with the least significant bit in the first position of the list).


First of all why make it recursive?

As pointed by others, the result variable is local, so it "resets" every time the method is called.

Because computers are binary beasts, I'd change to something like this:

QList<bool> widgetInput::decToBin(int number) 
{ 
    QList<bool> result = new QList<bool>(); 
    while (number)
    {
       result.Add(number & 1);
       number = number >> 1;
    }
    return result;
}


QList<bool> result; is a local variable so there can be only values inserted in this method call. And it is only one.

There are few solutions:

  • Add QList like a method parameter and after that if will work fine
  • Or append returned value from recursive calls to your list

But these solutions have reverse order so you can choose what is better for you.

And by the way, you are missing return value at the end of the method. So compiler should announce a warning.


There is a more generic iterative solution (that doesn't need Qt):

NB1: You can ignore the "byte completion" part as well as the "#include ". It is just aimed at completing the string with zeros to always get a mutiple of 8 bits...

NB2: Utils is the name of the class where those functions are implemented... I was told that a reverse function already exists in the standard library... haven't tried yet.

NB3: If you don't reverse the string, the little endian will come first... so the normal case is littleEndianFirst = false

#include <string>
#include <math.h>

    static std::string decToBin(int number, bool littleEndianFirst)
    {
        std::string result = "";
        do
        {
            ((number & 1) == 0) ? result += "0" : result += "1";
            number >>= 1;
        } while(number);

        if(!littleEndianFirst)
        {
            result = Utils::reverse(result);
        }

        // byte completion
        unsigned int completeByteLength = (ceil(result.length()/8.0))*8;
        while(result.length() < completeByteLength)
        {
            littleEndianFirst ? result += "0" : result = "0" + result;
        }

        return result;
    }

    static std::string reverse(std::string to_reverse){
        std::string result;
        for (int i = to_reverse.length()-1; i >=0 ; i--)
            result += to_reverse[i];
        return result;
    }
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜