[转]使用C#实现长整型向任意编码的转换和逆转换

 

   1:   using System;
   2:   using System.Collections.Generic;
   3:   using System.Linq;
   4:   using System.Text;
   5:   
   6:   namespace ConsoleApplication1
   7:   {
   8:   
   9:       public class Number
  10:       {
  11:           public string Characters
  12:           {
  13:               get;
  14:               set;
  15:           }
  16:   
  17:           public int Length
  18:           {
  19:               get
  20:               {
  21:                   if (Characters != null)
  22:                       return Characters.Length;
  23:                   else
  24:                       return 0;
  25:               }
  26:   
  27:           }
  28:   
  29:           public Number()
  30:           {
  31:               Characters = "0123456789";
  32:           }
  33:   
  34:           public Number(string characters)
  35:           {
  36:               Characters = characters;
  37:           }
  38:   
  39:           /// <summary>
  40:   /// 数字转换为指定的进制形式字符串
  41:   /// </summary>
  42:   /// <param name="number"></param>
  43:   /// <returns></returns>
  44:           public string ToString(long number)
  45:           {
  46:               List<string> result = new List<string>();
  47:               long t = number;
  48:   
  49:               while (t > 0)
  50:               {
  51:                   var mod = t % Length;
  52:                   t = Math.Abs(t / Length);
  53:                   var character = Characters[Convert.ToInt32(mod)].ToString();
  54:                   result.Insert(0, character);
  55:               }
  56:   
  57:               return string.Join("", result.ToArray());
  58:           }
  59:   
  60:           /// <summary>
  61:   /// 指定字符串转换为指定进制的数字形式
  62:   /// </summary>
  63:   /// <param name="str"></param>
  64:   /// <returns></returns>
  65:           public long FromString(string str)
  66:           {
  67:               long result = 0;
  68:               int j = 0;
  69:               foreach (var ch in new string(str.ToCharArray().Reverse().ToArray()))
  70:               {
  71:                   if (Characters.Contains(ch))
  72:                   {
  73:                       result += Characters.IndexOf(ch) * ((long)Math.Pow(Length, j));
  74:                       j++;
  75:                   }
  76:               }
  77:               return result;
  78:           }
  79:   
  80:       }
  81:   
  82:       
  83:   
  84:       class Program
  85:       {
  86:           static void Print(long number, Number adapter)
  87:           {
  88:               Console.WriteLine("输入数字:{0}", number);
  89:               Console.WriteLine("规则:{0}\t\t进制:{1}进制", adapter.Characters,adapter.Length);
  90:               var numtostr = adapter.ToString(number);
  91:               Console.WriteLine("转换结果:{0}", numtostr);
  92:               var strtonum = adapter.FromString(numtostr);
  93:               Console.WriteLine("逆向转换结果:{0}", strtonum);
  94:               Console.WriteLine();
  95:               Console.WriteLine("============ 无聊的分割线 ============");
  96:               Console.WriteLine();
  97:           }
  98:   
  99:           static void Main(string[] args)
 100:           {
 101:               //传统的2进制
 102:               Number n1 = new Number("01");
 103:               //传统的8进制
 104:               Number n2 = new Number("01234567");
 105:               //传统的16进制
 106:               Number n3 = new Number("0123456789ABCDEF");
 107:               //自定义编码的N进制,这个可以用来做验证码?
 108:               Number n4 = new Number("爹妈说名字太长躲在树后面会被部落发现");
 109:               //山寨一个短网址
 110:               Number n5 = new Number("0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ");
 111:   
 112:               Print(65535, n1);
 113:               Print(65535, n2);
 114:               Print(65535, n3);
 115:               Print(65535, n4);
 116:               Print(165535, n5);
 117:               
 118:               Console.ReadKey();
 119:   
 120:           }
 121:       }
 122:   }


原文地址:
http://www.cnblogs.com/linxuanchen/archive/2012/02/02/2336099.html

posted @ 2012-09-28 13:18  ChaoSimple  阅读(797)  评论(0编辑  收藏  举报