将十进制数转换为二进制

时间:2019-06-01 08:08:36

标签: c++

我正在编写一个将十进制数转换为二进制数的程序,但是该程序给出了3位或更多数字的垃圾值,我不明白为什么

int main(){
    string input;
    getline(cin,input);
    DecimanToBinary(input);
    return 0;
}
void DecIntToBin(string decimalInput){
    int decimalInteger=stoi(decimalInput);
    int digits=decimalInput.length();
    int binaryLength=pow(2,digits);
    int *binaryNum=new int[binaryLength];
    int remainder=0;
    int counter=binaryLength-1;
    while(decimalInteger>0 && counter>=0){
        remain=decimalInteger%2;
        binaryNum[counter]=remainder;
        decimalInteger/=2;
        j--;
    }
    for(int i=0;i<binarylength;i++){
        cout<<binaryNum[i];
    }
    delete[] binaryNum;
}

当输入在0-99范围内时,我得到二进制表示,但是 当输入是100而不是1100100时,我得到 843632 01100100

那843632是哪里来的?

1 个答案:

答案 0 :(得分:0)

int digits=decinp.length();
int binaryLength=pow(2,digits);

什么是 decinp

以2为底的数字位数受log2(decimalInteger)+1的限制,因为您假定的长度是错误的,所以您从 binaryNum

中写入未初始化的条目

在其他地方 j 必须是 counter ,而 remain 必须是 remainder

您的代码的更正版本可以是:

#include <iostream>
#include <string>
#include <math.h>
#include "errno.h"

using namespace std;

void DecIntToBin(string decimalInput)
{
  errno = 0;

  size_t idx;
  int decimalInteger = stoi(decimalInput, &idx);

  if ((errno != 0) || 
       (idx != decimalInput.size()) ||
      (decimalInteger < 0))
    cerr << "not a valid unsigned int" << endl;
  else if (decimalInteger == 0)
    cout << "0" << endl;
  else {
    int binaryLength = (int) log2(decimalInteger) + 1;
    int * binaryNum = new int[binaryLength];
    int counter = binaryLength-1;
    int remainder;

    while (decimalInteger > 0) {
      remainder = decimalInteger%2;
      binaryNum[counter] = remainder;
      decimalInteger/=2;
      counter--;
    }

    while (++counter < binaryLength){
      cout << binaryNum[counter];
    }
    cout << endl;

    delete[] binaryNum;
  }
}

int main(int, char ** argv)
{
  while (*(++argv) != NULL){
    cout << *argv << " => ";
    DecIntToBin(*argv);
  }

  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ g++ -g -pedantic -Wall -Wextra c.cc
pi@raspberrypi:/tmp $ ./a.out 0 1 2 3 7 8 99 1234
0 => 0
1 => 1
2 => 10
3 => 11
7 => 111
8 => 1000
99 => 1100011
1234 => 10011010010
pi@raspberrypi:/tmp $ 

valgrind 下执行:

pi@raspberrypi:/tmp $ valgrind ./a.out 0 1 2 3 7 8 99 1234
==3113== Memcheck, a memory error detector
==3113== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==3113== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==3113== Command: ./a.out 0 1 2 3 7 8 99 1234
==3113== 
0 => 0
1 => 1
2 => 10
3 => 11
7 => 111
8 => 1000
99 => 1100011
1234 => 10011010010
==3113== 
==3113== HEAP SUMMARY:
==3113==     in use at exit: 0 bytes in 0 blocks
==3113==   total heap usage: 9 allocs, 9 frees, 21,368 bytes allocated
==3113== 
==3113== All heap blocks were freed -- no leaks are possible
==3113== 
==3113== For counts of detected and suppressed errors, rerun with: -v
==3113== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 6 from 3)
pi@raspberrypi:/tmp $ 

但是使用std::vector :

更实用
#include <iostream>
#include <string>
#include <vector>
#include "errno.h"

using namespace std;

void DecIntToBin(string decimalInput)
{
  errno = 0;

  size_t idx;
  int decimalInteger = stoi(decimalInput, &idx);

  if ((errno != 0) || 
      (idx != decimalInput.size()) ||
      (decimalInteger < 0))
    cerr << "not a valid unsigned int" << endl;
  else if (decimalInteger == 0)
    cout << "0" << endl;
  else {
    vector<int> v;

    while (decimalInteger != 0) {
      v.push_back(decimalInteger & 1);
      decimalInteger >>= 1;
    }

    idx = v.size();
    do {
      cout << v[--idx];
    } while (idx != 0);
    cout << endl;
  }
}

int main(int, char ** argv)
{
  while (*(++argv) != NULL){
    cout << *argv << " => ";
    DecIntToBin(*argv);
  }

  return 0;
}

编译和执行:

pi@raspberrypi:/tmp $ g++ -pedantic -Wall -Wextra c.cc
pi@raspberrypi:/tmp $ ./a.out 0 1 99 100 1234
0 => 0
1 => 1
99 => 1100011
100 => 1100100
1234 => 10011010010