美文网首页unity3D技术分享
c#实现二进制和八进制十进制十六进制的相互转换

c#实现二进制和八进制十进制十六进制的相互转换

作者: 好怕怕 | 来源:发表于2022-04-14 21:53 被阅读0次
    image.png
    namespace ConsoleApplication5
    {
        class Program
        {
    
            static void Main(string[] args)
            {
                ChangeNum();
    
                Console.ReadLine();
            }
    
            private static void ChangeNum()
            {
                int number = 115342;
    
                string result = OnDECChange(number, 2);
                int r = OnSystemChange(result, 2);
                Console.WriteLine(number+"的2进制结果:" + result + " :" + r);
    
                result = OnDECChange(number, 8);
                r = OnSystemChange(result, 8);
                Console.WriteLine(number + "的8进制结果:" + result + " :" + r);
    
                result = OnDECChange(number, 10);
                r = OnSystemChange(result, 10);
                Console.WriteLine(number + "的10进制结果:" + result + " :" + r);
    
                result = OnDECChange(number, 16);
                r = OnSystemChange(result, 16);
                Console.WriteLine(number + "的16进制结果:" + result + " :" + r);
            }
    
            // 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
            // 0 1 2 3 4 5 6 7 8 9 A  B  C  D  E  F
    
            public static Dictionary<int, char> systemDic = new Dictionary<int, char>()
            {
                [0] = '0',
                [1] = '1',
                [2] = '2',
                [3] = '3',
                [4] = '4',
                [5] = '5',
                [6] = '6',
                [7] = '7',
                [8] = '8',
                [9] = '9',
                [10] = 'A',
                [11] = 'B',
                [12] = 'C',
                [13] = 'D',
                [14] = 'E',
                [15] = 'F',
            };
    
            /// <summary>
            /// 十进制数转二进制,八进制,十进制,十六进制
            /// </summary>
            /// <param name="number"></param>
            /// <param name="system"></param>
            /// <returns></returns>
            private static string OnDECChange(int number, int system)
            {
                List<int> resultList = new List<int>();
                while (true)
                {
                    if (number < system)
                    {
                        resultList.Add(number);
                        break;
                    }
                    int value = number / system;
                    int remainder = number - value * system;
                    number = value;
                    resultList.Add(remainder);
                }
                resultList.Reverse();
                StringBuilder sb = new StringBuilder();
                for (int i = 0; i < resultList.Count; i++)
                {
                    int r = resultList[i];
                    sb.Append(systemDic[r]);
                }
                return sb.ToString();
            }
    
    
            public static Dictionary<char, int> baseDic = new Dictionary<char, int>()
            {
                ['0'] = 0,
                ['1'] = 1,
                ['2'] = 2,
                ['3'] = 3,
                ['4'] = 4,
                ['5'] = 5,
                ['6'] = 6,
                ['7'] = 7,
                ['8'] = 8,
                ['9'] = 9,
                ['A'] = 10,
                ['B'] = 11,
                ['C'] = 12,
                ['D'] = 13,
                ['E'] = 14,
                ['F'] = 15,
            };
    
            /// <summary>
            /// 二进制数转八进制,十进制,十六进制
            /// </summary>
            /// <param name="number"></param>
            /// <param name="system"></param>
            /// <returns></returns>
            public static int OnSystemChange(string number, int system)
            {
                int result = 0;
                int index = 0;
                for (int i = number.Length - 1; i >= 0; i--)
                {
                    char key = number[i];
                    try
                    {
                        int value = baseDic[key];
                        result += value * (index == 0 ? 1 : (int)Math.Pow(system, index));
                        index++;
                    }
                    catch (Exception e)
                    {
                        Console.WriteLine(e);
                        throw;
                    }
    
                }
                return result;
          }
    }
    

    相关文章

      网友评论

        本文标题:c#实现二进制和八进制十进制十六进制的相互转换

        本文链接:https://www.haomeiwen.com/subject/nnpxertx.html