载入中,请稍候……

Base Convert from X to Y

Admin 于 2008-08-17 17:23:53 发表.NET

订阅: http://www.miniboke.com/Feed/Article_1.aspx
引用: http://www.miniboke.com/Trackback/hzlrLJUdDdtdoQccAKsq.aspx (UTF-8)
日期范围计算 < Base Convert from X to Y > 没有了

 

任意进制间转换的C#代码。可用随机密码生成、验证码生成。数据库表ID转短字符串。

  1. const string BaseString = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_@,./;'[]\\=-`<>?:\"{}|+~!#$%^&*()";  
  2. static int MaxBase = BaseString.Length;  
  3. static int MinBase = 2;  
  4. string Base10toX(int baseX, string base10Str)  
  5. {  
  6.     base10Str = base10Str.Trim();  
  7.     if (base10Str == ""return "";  
  8.     int base10Int = int.Parse(base10Str);  
  9.     //if(int.Parse(m)!=m){  
  10.     //    //alert("请输入整数!");  
  11.     //    return "";  
  12.     //};  
  13.     string target = "";  
  14.     string a = BaseString.Substring(0, baseX);  
  15.     int b;  
  16.     while (base10Int != 0)  
  17.     {  
  18.         b = base10Int % baseX;  
  19.         target = a[b] + target;  
  20.         base10Int = (base10Int - b) / baseX;  
  21.     }  
  22.     return target;  
  23. }  
  24. int BaseXto10(int baseX, string baseXStr)  
  25. {  
  26.     baseXStr = baseXStr.Trim();  
  27.     if (baseXStr == ""return -1;  
  28.     string a = BaseString.Substring(0, baseX);  
  29.  
  30.     int target = 0, c = 1;  
  31.     for (int x = baseXStr.Length - 1; x > -1; x--)  
  32.     {  
  33.         target += c * (a.IndexOf(baseXStr[x]));  
  34.         c *= baseX;  
  35.     }  
  36.     return target;  
  37. }  
  38. string BaseXtoY(int baseX, int baseY, string baseXStr)  
  39. {  
  40.     int temp = BaseXto10(baseX * 1, baseXStr);  
  41.     if (temp == -1) return "";  
  42.     return Base10toX(baseY, temp.ToString());  

Base N converter (N = 10-62)

http://www.codeproject.com/KB/recipes/ExpandableHexConverter.aspx

被阅319次, 0投一票Base Convert
  • 看完了要说点啥么?
  • 昵称 (不填说不了话)
  • 信箱地址 (不会被公开,但是不填也说不了话)
  • 网址 (这个不填也成)

Powered by MiniBoke v2.0.0.8 Build 0828

Copyright © 2008 迷你博客. All rights reserved.

粤ICP备07500939号