Guest User

Untitled

a guest
Jan 24th, 2017
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.14 KB | None | 0 0
  1. namespace Alphabet
  2. {
  3. public class AlphabetTest
  4. {
  5. public static readonly string Alphabet = "abcdefghijklmnopqrstuvwxyz0123456789";
  6. public static readonly int Base = Alphabet.Length;
  7.  
  8. public static string Encode(int i)
  9. {
  10. if (i == 0) return Alphabet[0].ToString();
  11.  
  12. var s = string.Empty;
  13.  
  14. while (i > 0)
  15. {
  16. s += Alphabet[i % Base];
  17. i = i / Base;
  18. }
  19.  
  20. return string.Join(string.Empty, s.Reverse());
  21. }
  22.  
  23. public static int Decode(string s)
  24. {
  25. var i = 0;
  26.  
  27. foreach (var c in s)
  28. {
  29. i = (i * Base) + Alphabet.IndexOf(c);
  30. }
  31.  
  32. return i;
  33. }
  34.  
  35. public static void Main(string[] args)
  36. {
  37. // Simple test of encode/decode operations
  38. for (var i = 0; i < 10000; i++)
  39. {
  40. if (Decode(Encode(i)) != i)
  41. {
  42. System.Console.WriteLine("{0} is not {1}", Encode(i), i);
  43. break;
  44. }
  45. }
  46. }
  47. }
  48. }
Add Comment
Please, Sign In to add comment