Skip to content

Instantly share code, notes, and snippets.

@IvanIvanoff
Created February 9, 2016 18:26
Show Gist options
  • Save IvanIvanoff/73ff34854e47355bfc2c to your computer and use it in GitHub Desktop.
Save IvanIvanoff/73ff34854e47355bfc2c to your computer and use it in GitHub Desktop.
Converts from dec to other numeral systems
#include <iostream>
#include <assert.h>
const int NUM_SYSTEM = 2;
char arr[] = { '0','1','2','3','4','5','6','7','8','9','A','B','C','D','E','F', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P','Q','R','S','T','U','V','W', 'X','Y', 'Z' };
char* DecToNumSystem(unsigned int num)
{
assert(NUM_SYSTEM > 1);
int size = std::floor(log(num) / log(NUM_SYSTEM)) + 1;
char* res = new char[size];
res[size] = '\0';
int i = size - 1;
while (num > 0) {
res[i] = arr[num % NUM_SYSTEM];
num /= NUM_SYSTEM;
--i;
}
return res;
}
int main()
{
std::cout << DecToNumSystem(16222122) << std::endl;
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment