Advertisement
Guest User

Untitled

a guest
Jan 18th, 2017
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.44 KB | None | 0 0
  1. char* charset = " 0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  2. int charsetLength = strlen(charset);
  3.  
  4. std::string integerToKey(unsigned long long input)
  5. {
  6. unsigned long long num = input;
  7. string key = "";
  8.  
  9. while(num)
  10. {
  11. key += charset[num % charsetLength];
  12. num /= charsetLength;
  13. }
  14.  
  15. return key;
  16. }
  17.  
  18. // Create the integer to key conversion lookup table
  19. int lookupChars;
  20.  
  21. if(lookupDisabled)
  22. lookupChars = 1;
  23. else
  24. largeLookup ? lookupChars = 4 : lookupChars = 2;
  25.  
  26. lookupSize = pow(charsetLength, lookupChars);
  27. integerToKeyLookup = new char*[lookupSize];
  28.  
  29. for(unsigned long i = 0; i < lookupSize; i++)
  30. {
  31. unsigned long num = i;
  32. int j = 0;
  33.  
  34. integerToKeyLookup[i] = new char[lookupChars];
  35.  
  36. while(num)
  37. {
  38. integerToKeyLookup[i][j] = charset[num % charsetLength];
  39. num /= charsetLength;
  40.  
  41. j++;
  42. }
  43.  
  44. // Null terminate the string
  45. integerToKeyLookup[i][j] = '';
  46. }
  47.  
  48. std::string integerToKey(unsigned long long input)
  49. {
  50. unsigned long long num = input;
  51. string key = "";
  52.  
  53. while(num)
  54. {
  55. key += integerToKeyLookup[num % lookupSize];
  56. num /= lookupSize;
  57. }
  58.  
  59. return key;
  60. }
  61.  
  62. const char* charset = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
  63. #define CHARSET_LENGTH 62
  64.  
  65. // maximum size = 11 chars
  66. void integerToKey(char result[13], unsigned long long input)
  67. {
  68. char* p = result;
  69. while(input > 0)
  70. {
  71. *p++ = charset[input % CHARSET_LENGTH];
  72. input /= CHARSET_LENGTH;
  73. }
  74.  
  75. // null termination
  76. *p = '';
  77. // need to reverse the output
  78. char* o = result;
  79. while(o + 1 < p)
  80. swap(*++o, *--p);
  81. }
  82.  
  83. std::string ullToString(unsigned long long v, int base = 64) {
  84. assert(base < 65);
  85. assert(base > 1);
  86. static const char digits[]="0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ+/";
  87. const int max_length=65;
  88. static char buffer[max_length];
  89.  
  90. buffer[max_length-1]=0;
  91. char *d = buffer + max_length-1;
  92. do {
  93. d--;
  94. int remainder = v % base;
  95. v /= base;
  96. *d = digits[remainder];
  97. } while(v>0);
  98.  
  99. return d;
  100. }
  101.  
  102. size_t base64_encode(char* outbuffer, size_t maxoutbuflen, const char* inbuffer, size_t inbuflen);
  103.  
  104. std::string integerToKey(unsigned long long input) {
  105. char buffer[14];
  106. size_t len = base64_encode(buffer, sizeof buffer, (const char*)&input, sizeof input);
  107. return std::string(buffer, len);
  108. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement