Advertisement
Guest User

Untitled

a guest
Nov 24th, 2014
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.88 KB | None | 0 0
  1. npm install int-encoder
  2.  
  3. var en = require('int-encoder');
  4.  
  5. //simple integer conversion
  6. en.encode(12345678); // "ZXP0"
  7. en.decode('ZXP0'); // 12345678
  8.  
  9. //convert big hex number using optional base argument
  10. en.encode('e6c6b53d3c8160b22dad35a0f705ec09', 16); // 'hbDcW9aE89tzLYjDgyzajJ'
  11. en.decode('hbDcW9aE89tzLYjDgyzajJ', 16); // 'e6c6b53d3c8160b22dad35a0f705ec09'
  12.  
  13. public static class ShortCodes
  14. {
  15. private static Random rand = new Random();
  16.  
  17. // You may change the "shortcode_Keyspace" variable to contain as many or as few characters as you
  18. // please. The more characters that aer included in the "shortcode_Keyspace" constant, the shorter
  19. // the codes you can produce for a given long.
  20. const string shortcode_Keyspace = "abcdefghijklmnopqrstuvwxyz0123456789";
  21.  
  22. // Arbitrary constant for the maximum length of ShortCodes generated by the application.
  23. const int shortcode_maxLen = 12;
  24.  
  25.  
  26. public static string LongToShortCode(long number)
  27. {
  28. int ks_len = shortcode_Keyspace.Length;
  29. string sc_result = "";
  30. long num_to_encode = number;
  31. long i = 0;
  32. do
  33. {
  34. i++;
  35. sc_result = shortcode_Keyspace[(int)(num_to_encode % ks_len)] + sc_result;
  36. num_to_encode = ((num_to_encode - (num_to_encode % ks_len)) / ks_len);
  37. }
  38. while (num_to_encode != 0);
  39. return sc_result;
  40. }
  41.  
  42.  
  43. public static long ShortCodeToLong(string shortcode)
  44. {
  45. int ks_len = shortcode_Keyspace.Length;
  46. long sc_result = 0;
  47. int sc_length = shortcode.Length;
  48. string code_to_decode = shortcode;
  49. for (int i = 0; i < code_to_decode.Length; i++)
  50. {
  51. sc_length--;
  52. char code_char = code_to_decode[i];
  53. sc_result += shortcode_Keyspace.IndexOf(code_char) * (long)(Math.Pow((double)ks_len, (double)sc_length));
  54. }
  55. return sc_result;
  56. }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement