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; } }
id生成唯一邀请码,及解码转成id
此随笔或为自己所写、或为转载于网络。仅用于个人收集及备忘。