public partial class InvitationCodeHelper
    {
        private const int CodeLenth = 6;
        private static char[] Source_Chars = new char[] { '9', 'P', 'L', 'M', '8', 'K', 'N', '7', 'J', 'B', '6', 'U', 'H', 'V', '5', 'T', 'G', 'C', '4', 'R', 'F', '3', 'E', 'D', 'Z', '2', 'W', 'S', 'Q', 'A' };
        private static char Default_Char = 'X';

        private static int Source_Chars_Lenght { get { return Source_Chars.Length; } }

        public static string IdToCode(long id)
        {
            long tempId = id;
            string code = "";
            long mod = 0;
            int length = Source_Chars_Lenght;
            StringBuilder sb = new StringBuilder();
            while (tempId > 0)
            {
                mod = tempId % length;
                tempId = (tempId - mod) / length;
                code = Source_Chars[mod] + code;

            }
            return code.PadRight(CodeLenth, Default_Char);
        }

        public static long? CodeToId(string code)
        {
            if (string.IsNullOrWhiteSpace(code)) return null;
            string temp = code.ToUpper();
            int length = Source_Chars_Lenght;
            temp = new string((from s in temp where s != Default_Char select s).ToArray());
            long id = 0;
            for (int i = 0; i < temp.ToCharArray().Length; i++)
            {
                for (int j = 0; j < length; j++)
                {
                    if (temp.ToCharArray()[i] == Source_Chars[j])
                    {
                        id += j * Convert.ToInt64(Math.Pow(length, temp.ToCharArray().Length - i - 1));
                    }
                }
            }
            return id;
        }
    }

 

相关文章:

  • 2021-08-25
  • 2021-07-21
  • 2021-08-13
  • 2022-12-23
  • 2022-12-23
  • 2021-08-09
猜你喜欢
  • 2022-12-23
  • 2021-08-21
  • 2022-12-23
  • 2022-01-19
  • 2022-12-23
  • 2021-08-09
相关资源
相似解决方案