一个开源的Asp.net2.0博客系统
任意进制间转换的C#代码。可用随机密码生成、验证码生成。数据库表ID转短字符串。
- const string BaseString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@,./;'[]\\=-`<>?:\"{}|+~!#$%^&*()";
- static int MaxBase = BaseString.Length;
- static int MinBase = 2;
- string Base10toX(int baseX, string base10Str)
- {
- base10Str = base10Str.Trim();
- if (base10Str == "") return "";
- int base10Int = int.Parse(base10Str);
- //if(int.Parse(m)!=m){
- // //alert("请输入整数!");
- // return "";
- //};
- string target = "";
- string a = BaseString.Substring(0, baseX);
- int b;
- while (base10Int != 0)
- {
- b = base10Int % baseX;
- target = a[b] + target;
- base10Int = (base10Int - b) / baseX;
- }
- return target;
- }
- int BaseXto10(int baseX, string baseXStr)
- {
- baseXStr = baseXStr.Trim();
- if (baseXStr == "") return -1;
- string a = BaseString.Substring(0, baseX);
- int target = 0, c = 1;
- for (int x = baseXStr.Length - 1; x > -1; x--)
- {
- target += c * (a.IndexOf(baseXStr[x]));
- c *= baseX;
- }
- return target;
- }
- string BaseXtoY(int baseX, int baseY, string baseXStr)
- {
- int temp = BaseXto10(baseX * 1, baseXStr);
- if (temp == -1) return "";
- return Base10toX(baseY, temp.ToString());
- }
Base N converter (N = 10-62)
http://www.codeproject.com/KB/recipes/ExpandableHexConverter.aspx