" Life is not a problem to be solved but a reality to be experienced! "
© Copyright 2013 - 2024 KUDO.TIPS - All rights reserved.
#include <iomanip>
#include <iostream>
#include <string>
using namespace std;
#define DBG(x) { cout << left << setw(40) << #x << (x) << endl; }
string octToBin(string const& oct)
{
string ret;
for(unsigned i = 0; i < (oct.size() + 1)/3; ++i){
int n = 0;
for(int j = 0; j < 3 && 3*i + j < oct.size(); ++j){
char c = oct[oct.size() - 3 * i - j - 1];
n += (c - '0') << j;
}
ret.insert(0, 1, char(n + '0'));
}
return ret;
}
int main()
{
DBG(octToBin("10110111"));
}