-2

我正在用 C++ 和 C++ 的 Qt 库制作一个 IRC 机器人。我环顾四周,但找不到我喜欢的二进制到十进制转换的算法。我的十进制到二进制根本不起作用,但我的 bin 到 dec 返回一个答案,转移到提升 2 位。这是我的代码:

QString MainWindow::toBin(bool isneg, QString num) {
QString buffer;
bool boolBuffer;
int intBuffer;
int bitNum;
if(isneg) {
    boolBuffer = true;
    intBuffer = 0;
    QString out;
    while(boolBuffer) {
        if(pow(2, (double)intBuffer) >= num.toDouble()) {
            boolBuffer = false;
        } else {
            intBuffer++;
        }
    }

    bitNum = intBuffer;
    intBuffer = num.toInt();
    buffer = "1";
    intBuffer = intBuffer - pow(2, bitNum);
    bitNum--;
    for(int i=bitNum;i>=0;i--) {
        if(intBuffer - pow(2, i) >= 0) {
            intBuffer = intBuffer - pow(2, i);
            buffer.append("1");
        } else {
            buffer.append("0");
        }
    }

    for(int i=0;i<buffer.length();i++) {
        if(buffer.at(i)=='1') out.append("0");
        else out.append("1");
    }

    out.append(" + 1");
    return out;
} else {
   boolBuffer = true;
   intBuffer = 0;
   while(boolBuffer) {
       if(pow(2, (double)intBuffer) >= num.toDouble()) {
           boolBuffer = false;
       } else {
           intBuffer++;
       }
   }

   bitNum = intBuffer;
   intBuffer = num.toInt();
   buffer = "1";
   intBuffer = intBuffer - pow(2, bitNum);
   bitNum--;
   for(int i=bitNum;i>=0;i--) {
       if(intBuffer - pow(2, i) >= 0) {
           intBuffer = intBuffer - pow(2, i);
           buffer.append("1");
       } else {
           buffer.append("0");
       }
   }

   return buffer;
}
}

QString MainWindow::toDec(bool isneg, QString num) {
QString buffer;
int intBuffer;
if(isneg) {
    buffer = num;
    intBuffer = 0;
    for(int i=0;i<buffer.length();i++) {
        if(buffer.at(i)=='1') intBuffer = intBuffer + pow(2, abs(buffer.length()-i-1));
    }
    buffer = QString::number(-1*intBuffer);

    return buffer;
} else {
    buffer = num;
    intBuffer = 0;
    for(int i=0;i<buffer.length();i++) {
        if(buffer.at(i)=='1') intBuffer = intBuffer + pow(2, abs(buffer.length()-i-1));
    }
    buffer = QString::number(intBuffer);
    ui->textBrowser->append(buffer);
    return buffer;
}
}

其他一切正常,我已经对其进行了测试并调用了它,消息的发送很好,我已经完成并手动输入了 QString“10”,然后在写下 vars 的同时在脑海中运行代码文档,我得出了结论 2,但是当我调用该函数时,它返回 8。

4

1 回答 1

1

You can approach this differently. Instead of trying to compute the bit value at each string position, just double your number every time you get a new digit. This is what is done by most algorithms out there (you know, the ones you didn't like):

QString MainWindow::toDec(bool isneg, QString num) {
    int val = 0;

    for( int i = 0; i < num.length(); i++ ) {
        val = (val << 1) + num.at(i).digitValue();
    }

    return QString::number( isneg ? -val: val);
}

I'm not sure about your "negative" representation though. Binary negatives are usually in two's-complement form. This decimal conversion will not work for that.


[edit] You know, I just had a thought that your input is not sensible. Both your algorithm and my simplification rely on the string containing only 0 or 1 characters. But I'd be willing to bet you have other characters in that string.

The fix for this would demand a modification of the loop:

for( int i = 0; i < num.length(); i++ )
{
   int bit = num.at(i).digitValue();
   if( bit != 0 && bit != 1 ) break;
   val = (val << 1) + bit;
}
于 2013-09-22T23:25:56.737 回答