31 lines
432 B
C++
31 lines
432 B
C++
#include "binary.h"
|
|
|
|
namespace binary {
|
|
|
|
unsigned convert(const std::string &input)
|
|
{
|
|
unsigned result = 0;
|
|
unsigned factor = 1;
|
|
|
|
for (auto i = input.crbegin(); i != input.crend(); ++i) {
|
|
|
|
switch (*i) {
|
|
|
|
case '0':
|
|
break;
|
|
|
|
case '1':
|
|
result += factor;
|
|
break;
|
|
|
|
default:
|
|
return 0;
|
|
}
|
|
|
|
factor <<= 1;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
}
|