Skip to content

Instantly share code, notes, and snippets.

@farnoy
Created November 13, 2012 19:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save farnoy/4067799 to your computer and use it in GitHub Desktop.
Save farnoy/4067799 to your computer and use it in GitHub Desktop.
Glib::ustring NumberConverter::get_result() const {
std::stringstream output;
long intermediate = 0;
switch (_info.from) {
case NumberSystem::BASE_2:
intermediate = strtol(_input.data(), NULL, 2);
break;
case NumberSystem::BASE_8:
intermediate = strtol(_input.data(), NULL, 8);
break;
case NumberSystem::BASE_10:
intermediate = strtol(_input.data(), NULL, 10);
break;
case NumberSystem::BASE_16:
intermediate = strtol(_input.data(), NULL, 16);
break;
}
switch (_info.to) {
case NumberSystem::BASE_8:
output << std::oct << intermediate;
break;
case NumberSystem::BASE_16:
output << std::hex << intermediate;
break;
case NumberSystem::BASE_10:
output << std::dec << intermediate;
break;
case NumberSystem::BASE_2:
auto str = std::bitset<16>(intermediate).to_string();
for (auto it = str.begin(); it != str.end(); it++)
if (*it == '1' || it+1 == str.end()) { str = std::string(it, str.end()); break; }
output << str;
break;
}
assert(output.str().size() > 0);
return output.str();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment