在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
private const string initValue = "A0000001"; private static string cs = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"; public static string AddOne(string str) { if (str == string.Empty) { return initValue; } Int64 num = StringToNumber(str); return NumberToString(++num); } public static Int64 StringToNumber(string str) { int leg = str.Length; double num = 0; if (leg != 0) { for (int i = 0; i < leg; i++) { if (str[i] != '0') { num += cs.IndexOf(str[i]) * Math.Pow(36, leg - 1 - i); } } } return Convert.ToInt64(num); } public static string NumberToString(Int64 num) { string str = string.Empty; while (num >= 36) { str = cs[(int)(num % 36)] + str; num = num / 36; } return cs[(int)num] + str; } 2019-8-30改进的版本 class Program { static void Main(string[] args) { string curr = "ZZZZZZ"; Console.WriteLine("原始值:" + curr); Int64 num = Scale.ToInt64(curr); Console.WriteLine("转成Int64:" + num); Console.WriteLine("根据Int64转成进制:" + Scale.ToCurr(num)); Console.ReadLine(); } } public static class Scale { /// <summary> /// 进制符号字符串 /// </summary> private static string scString = "0123456789ABCDEFGHJKLMNPQRSTUVWXYZ"; /// <summary> /// 字符集,可以根据编号索引拿到字符 /// </summary> private static char[] scArray = scString.ToCharArray(); /// <summary> /// 字符字典,可以根据字符拿到编号索引 /// </summary> private static Dictionary<char, int> scDic = ToCharDic(); /// <summary> /// 根据字符串反馈进制数 /// </summary> public static int Len { get { return scString.Length; } } /// <summary> /// 将字符串处理成字符字典 /// </summary> private static Dictionary<char, int> ToCharDic() { Dictionary<char, int> dic = new Dictionary<char, int>(); for (int i = 0; i < scArray.Length; i++) { dic.Add(scArray[i], i); } return dic; } /// <summary> /// 根据传入的字符符号定义进制,字符符号不能重复,模拟十进制字符串为:0123456789 /// </summary> public static void SetScale(string scaleString) { scString = scaleString; scArray = scString.ToCharArray(); scDic = ToCharDic(); } /// <summary> /// 将Int64转成当前进制字符串 /// </summary> public static string ToCurr(long num) { string curr = ""; while (num >= Len) { curr = scArray[num % Len] + curr; num = num / Len; } curr = scArray[num] + curr; return curr; } /// <summary> /// 将当前进制字符串转成Int64 /// </summary> public static long ToInt64(string curr) { double num = 0; for (int i = 0; i < curr.Length; i++) { num += scDic[curr[i]] * Math.Pow(Len, curr.Length - 1 - i); } return Convert.ToInt64(num); } }
|
2023-10-27
2022-08-15
2022-08-17
2022-09-23
2022-08-13
请发表评论