Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
61
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.83 KB | None | 0 0
  1. public static class Base26Converter
  2. {
  3. private const string CharList = "abcdefghijklmnopqrstuvwxyz";
  4.  
  5. public static String Encode(long input)
  6. {
  7. if (input < 0) throw new ArgumentOutOfRangeException("input", input, "input cannot be negative");
  8.  
  9. char[] clistarr = CharList.ToCharArray();
  10. var result = new Stack<char>();
  11. while (input != 0)
  12. {
  13. result.Push(clistarr[input % 26]);
  14. input /= 26;
  15. }
  16. return new string(result.ToArray());
  17. }
  18.  
  19. public static Int64 Decode(string input)
  20. {
  21. var reversed = input.ToLower().Reverse();
  22. long result = 0;
  23. int pos = 0;
  24. foreach (char c in reversed)
  25. {
  26. result += CharList.IndexOf(c) * (long)Math.Pow(26, pos);
  27. pos++;
  28. }
  29. return result;
  30. }
  31. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement