fork(3) download
  1. #include <cstdio>
  2. #include <cstring>
  3. #include <cstdlib>
  4. #include <ctype.h>
  5.  
  6. namespace prism
  7. {
  8. typedef unsigned long long int token_t;
  9.  
  10. struct _ulldiv_t
  11. {
  12. unsigned long long int quot;
  13. unsigned long long int rem;
  14.  
  15. _ulldiv_t() : quot(0ull), rem(0ull) { }
  16. };
  17.  
  18. _ulldiv_t _div(unsigned long long int num, unsigned long long int divider)
  19. {
  20. _ulldiv_t result;
  21. result.rem = num%divider;
  22. result.quot = (unsigned long long int)num / divider;
  23. return result;
  24. }
  25.  
  26. unsigned long long int powull(unsigned long long int base, long num)
  27. {
  28. unsigned long long int result = 1;
  29. for (long i = 0; num > i; ++i)
  30. result = result * base;
  31. return result;
  32. }
  33.  
  34. const int g_iNumLetters = 38;
  35. const char g_pszLetters[g_iNumLetters] = "\00123456789abcdefghijklmnopqrstuvwxyz_";
  36.  
  37. void token_to_string(char* dest, const prism::token_t& token)
  38. {
  39. prism::token_t tokenCopy = token;
  40.  
  41. _ulldiv_t result;
  42. int i = 0;
  43. for (;tokenCopy != 0; ++i)
  44. {
  45. result = _div(tokenCopy, 38ull);
  46. tokenCopy = result.quot;
  47. dest[i] = g_pszLetters[result.rem];
  48. }
  49. dest[i] = '\0';
  50. }
  51.  
  52. int get_id_char(char cLetter)
  53. {
  54. for (int i = 0; i < g_iNumLetters; ++i)
  55. if (cLetter == g_pszLetters[i])
  56. return i;
  57. return 0;
  58. }
  59.  
  60. token_t string_to_token(const char* str)
  61. {
  62. token_t result = 0;
  63. unsigned len = strlen(str);
  64. for (unsigned i = 0; i < len; i++)
  65. result += powull(38, i) * (unsigned long long)get_id_char(tolower(str[i]));
  66. return result;
  67. }
  68. }
  69.  
  70. int main(int, char**)
  71. {
  72. const char * szStr = "test_szyfr";
  73. prism::token_t Result = prism::string_to_token(szStr);
  74. printf("result = %llu\n", Result);
  75.  
  76. char Buffer[16] = { 0 };
  77. prism::token_to_string(Buffer, Result);
  78. printf("result = %s\n", Buffer);
  79. return 0;
  80. }
Success #stdin #stdout 0s 3096KB
stdin
Standard input is empty
stdout
result = 4360368022740806
result = test_szyfr