任意進位制轉換演算法

suifei發表於2016-05-31
任意進位制轉換演算法

N年沒有寫部落格,發個進位制轉換的玩下,支援負數;功能屬於簡化版的 Convert.ToInt32 ,特點是:

1、任意位,如:0,1(二進位制),0...7(八進位制),0...9,A...F(16進位制),0...N(N進位制),或者是:!@#$%^&*(8進位制,字元符號),也可以是中文。

2、8 位最大長度。

3、C#原始碼。

最近寫markdown格式習慣啦,cnblogs啥時候全改掉算了,別用這個htmleditor算了。

先說明下進位制轉換的基礎知識,不會的可以瞭解下,會的就別看了,後面的也別看,純粹屬於浪費時間。

 

 十六進位制轉十進位制表
 
                                           10         15        1
<--------------------------------------------------------------------
0         0         0        0      0      A         F         1
<--------------------------------------------------------------------
16^7       16^6      16^5     16^4   16^3  16^2       16^1      16^0
268435456  16777216  1048576  65536  4096  256        16        1
<--------------------------------------------------------------------
                                           (10*256) + (15*16) + (1*1)
                                           =2560+240+1
                                           =2801

  

 十進位制轉十六進位制表

          ^
19%16=3   | 0x3
19/16=1   | 0x1
          = 0x13H

 

 十六進位制到二進位制快速轉換

 <-----------------------
 2^3   2^2  2^1  2^0
 8     4    2    1
 <-----------------------
 0xF821 = 1111 1000 0010 0001
 <-----------------------
 0xF = 15
     = 8 + 4 + 2 + 1
     = 1   1   1   1
 0x8 = 8
     = 8 + 0 + 0 + 0
     = 1   0   0   0
 0x2 = 2
     = 0 + 0 + 2 + 0
     = 0   0   1   0
 0x1 = 1
     = 0 + 0 + 0 + 1
     = 0   0   0   1

 

 二進位制到十六進位制快速轉換
 <-----------------------
 2^3   2^2  2^1  2^0
 8     4    2    1
 <-----------------------
 1111 1000 0010 0001 = 0xF821
 <-----------------------
 1111    = 8 + 4 + 2 + 1
        = 15
        = 0xF
 1000    = 8 + 0 + 0 + 0
        = 8
        = 0x8
 0010    = 0 + 0 + 2 + 0
        = 2
        = 0x2
 0001    = 0 + 0 + 0 + 1
        = 1
        = 0x1

 

十進位制快速轉換十六進位制
        103 = (6 * 16) + 7 = 0x67
        22  = (1 * 16) + 6 = 0x16
        54  = (3 * 16) + 6 = 0x36
        255 = (15* 16) + 15 = 0xff
        999 = (62 * 16) + 7 ~ 0x7
          62= (3 * 16) + 14 ~ 0xe
            = 3 ~ 0x3
            = 0x3e7
        9999= (624 * 16) + 15 ~ 0xF
         624= (39*16) + 0 ~ 0x0
          39= (2* 16) + 7 ~ 0x7
           2= 2 ~ 0x2
            = 0x270f
        1980= (123 * 16) + 12 ~ 0xc
         123= (7 * 16) + 11 ~ 0xb
           7= 7 ~ 0x7
            = 0x7bc

 

計算過程擺完了,下面是測試程式碼(程式碼未經優化,純屬測試):

    class MainClass
    {
        public static void Main (string[] args)
        {
            var nt = -3212;
            var ct = "";
            ct = ConvertToAny (nt, 0, "a,b,c");
            Console.WriteLine (ct);
            var rt = AnyToNumber (ct, "a,b,c");
            Console.WriteLine (rt);
            Console.WriteLine (Convert.ToString(nt,2));
            Console.ReadLine ();

            Console.WriteLine ("Test begin");
            for (int n = -1230; n < 1230; n++) {
                var a = n.ToString ("X");
                var b = ConvertToAny (n, 0, "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F");
                if (a != b) {
                    Console.WriteLine ("[{2}]Error:a!=b: {0} {1}", a, b, n);
                } else if (n % 100 == 0) {
                    Console.WriteLine ("[{2}]Curr: a!=b: {0} {1}  OK", a, b, n);
                }
            }
            Console.WriteLine ("Test ok");

            Console.WriteLine ("Test begin");
            for (int n = -1230; n < 1230; n++) {
                var hex = n.ToString ("X");
                var a = Convert.ToInt32 (hex, 16);
                var b = AnyToNumber (hex, "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F");
                if (a != b) {
                    Console.WriteLine ("[{2}]Error:a!=b: {0} {1}", a, b, hex);
                } else if (n % 100 == 0) {
                    Console.WriteLine ("[{2}]Curr: a!=b: {0} {1}  OK", a, b, hex);
                }
            }
            Console.WriteLine ("Test ok");
            Console.WriteLine (ConvertToAny (int.MaxValue, 0, "0,1,2,3,4,5,6,7,8,9,A,B,C,D,E,F"));
        }

        static bool isNegativeNumber(string any, string anyString ){
            var s = ConvertToAny (int.MaxValue, 0, anyString);
            if (s.Length != any.Length) {
                return false;
            } else {

                var ReverseTable = BuildReverseTable (anyString);

                if (ReverseTable [any [0].ToString ()] > ReverseTable [s [0].ToString ()]) {
                    return true;
                }
            }
            return false;
        }
        static int AnyToNumber (string any, string anyString = "0,1,2,3,4,5,6,7,8,9")
        {
            int sum = 0;
            var ConversionTable = BuildConversionTable (anyString);
            var ReverseTable = BuildReverseTable (anyString);
            var tableBit = ConversionTable.Count;

            bool negativeNumber = false;
            var conversionPadSize = 0;
            conversionPadSize = 128 / tableBit;
            if (tableBit < 10) {
                conversionPadSize /= 2;
            }
            //是否負數
            if( isNegativeNumber( any, anyString) ){
                negativeNumber = true;
                //反轉
                var res = any;
                any = "";
                var moveBit = tableBit - 1;
                foreach (var c in res) {
                    any += NumberConversion (ConversionTable, moveBit - FindConversion (ReverseTable, c.ToString ()));
                }
            }
            var cur = any.Length - 1;
            for (var n = 0; n < any.Length; n++,cur--) {
                var c = any [n].ToString ();

                var bitSum = FindConversion (ReverseTable, c) * (int)Math.Pow (tableBit, cur);
                sum += bitSum;
            }
            if (negativeNumber) {
                //補位
                sum++;
                sum = 0 - sum;
            }
            return sum;
        }

        static string ConvertToAny (int number, int padSize = 8, string anyString = "0,1,2,3,4,5,6,7,8,9")
        {
            var conversionPadSize = padSize;

            var ConversionTable = BuildConversionTable (anyString);
            var ReverseTable = BuildReverseTable (anyString);
            var tableBit = ConversionTable.Count;

            long input = Math.Abs ((long)number);
            //補碼
            if (number < 0) {
                input -= 1;

                conversionPadSize = 128 / tableBit;
                if (tableBit < 10) {
                    conversionPadSize /= 2;
                }
            }
            
            var result = "";
            while (true) {
                if (input >= tableBit) {
                    result = NumberConversion (ConversionTable, (int)(input % tableBit)) + result;
                    input = (int)(input / tableBit);
                } else {
                    result = NumberConversion (ConversionTable, (int)input) + result;
                    break;
                }
            }

            if (result.Length < conversionPadSize) {
                //補位
                result = StringPadLeft (result, conversionPadSize, ConversionTable [0]);
            } else {
//                //對齊
//                if (result.Length % 2 != 0) {
//                    result = ConversionTable[0] + result;
//                }
            }    

            if (number < 0) {
                //反轉
                var res = result;
                result = "";
                var moveBit = tableBit - 1;
                foreach (var c in res) {
                    result += NumberConversion (ConversionTable, moveBit - FindConversion (ReverseTable, c.ToString ()));
                }
            }

            return result;
        }

        static string StringPadLeft (string src, int size, string c)
        {
            var nsize = size - src.Length;
            for (var n = 0; n < nsize; n++) {
                src = c + src;
            }
            return src;
        }

        static string NumberConversion (Dictionary<int,string> table, int n)
        {
            return table [n];
        }

        static int FindConversion (Dictionary<string,int> table, string c)
        {
            return table [c];
        }

        static Dictionary<string,int> BuildReverseTable (string tableString)
        {
            var table = tableString.Split (",".ToCharArray (), StringSplitOptions.RemoveEmptyEntries).ToList ();
            //補位操作,必須是2的倍數
            if (table.Count % 2 != 0) {
                table.Insert (0, "0");
            }
            var result = new Dictionary<string,int> ();
            for (var i = 0; i < table.Count; i++) {
                result.Add (table [i].ToString (), i);
            }
            return result;
        }

        static Dictionary<int,string> BuildConversionTable (string tableString)
        {
            var table = tableString.Split (",".ToCharArray (), StringSplitOptions.RemoveEmptyEntries).ToList ();
            //補位操作,必須是2的倍數
            if (table.Count % 2 != 0) {
                table.Insert (0, "0");
            }
            var result = new Dictionary<int,string> ();
            for (var i = 0; i < table.Count; i++) {
                result.Add (i, table [i].ToString ());
            }
            return result;
        }
    }

 

有趣的測試:

轉換結果

 

程式碼未經測試,自己可以完善

相關文章