This week only. Pastebin PRO Accounts Christmas Special! Don't miss out!Want more features on Pastebin? Sign Up, it's FREE!
Guest

Numeric base conversions

By: a guest on Aug 11th, 2011  |  syntax: PHP  |  size: 1.10 KB  |  views: 97  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. /**
  2.  * C++ numeric base conversions
  3.  *
  4.  * - sangf
  5.  */
  6.  
  7. #include
  8. #include
  9. #include
  10.  
  11. #if INT_MAX == 2147483647 && UINT_MAX == 4294967295
  12.    typedef int int_32t;
  13.     typedef unsigned int uint_32t;
  14. #elif LONG_MAX == 2147483647 && ULONG_MAX == 4294967295
  15.    typedef int int_32t;
  16.     typedef unsigned int uint_32t;
  17. #else
  18. #   error 32-bit integers required for this code
  19. #endif
  20.  
  21.  
  22. static const char *base_lookup = \"0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ\";
  23. std::string dec_to_base(int_32t number, int_32t base);
  24.  
  25. std::string dec_to_base(int_32t number, int_32t base)
  26. {
  27.         if (base = 37) throw;
  28.         else
  29.         {
  30.                 bool negative = false;
  31.                 if (number < 0)
  32.                 {
  33.                         number -= (number * 2); // negation
  34.                         negative = true;
  35.                 }
  36.                 std::string hex_int_32t;
  37.                 int result = number, remain = 0;
  38.                 do
  39.                 {
  40.                         number = number / base; // division result
  41.                         remain = result % base; // remainder result
  42.                         result = number;
  43.                         hex_int_32t.insert(0, 1, base_lookup[remain]);
  44.  
  45.                 } while (result != 0);
  46.  
  47.                 if (negative) hex_int_32t.insert(0, 1, \'-\');
  48.                 return hex_int_32t;
  49.         }
  50. }
clone this paste RAW Paste Data