fork download
  1. #include <iostream>
  2. #include <string>
  3.  
  4. std::string radix (int value, unsigned r) {
  5. std::string buffer;
  6. std::string sign;
  7. if (value < 0) {
  8. sign = "-";
  9. value = -value;
  10. }
  11. do {
  12. int digit = value % r;
  13. buffer += digit<10 ? '0'+digit : 'A'+(digit-10);
  14. value /= r;
  15. } while (value !=0);
  16.  
  17. buffer += sign;
  18. return std::string(buffer.rbegin(), buffer.rend());
  19. }
  20.  
  21. int main()
  22. {
  23. std::cout << radix(5,2) << std::endl
  24. << radix(5,10) << std::endl
  25. << radix(-5,10) << std::endl
  26. << radix(15,2) << std::endl
  27. << radix(15,7) << std::endl
  28. << radix(15,8) << std::endl
  29. << radix(15,10) << std::endl
  30. << radix(15,14) << std::endl
  31. << radix(15,15) << std::endl
  32. << radix(15,16);
  33. }
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
101
5
-5
1111
21
17
15
11
10
F