天天看點

進制轉換

還是我太敏感了,感覺周末彈吉他被那個什麼了,好吧,帶上耳機調到50%以上的音樂聽歌也是不錯的選擇,暫時找不到想看的電影電視,也不敢再去找電視看了,太多了,也太長了,剛買回來的書還沒開始捂熱乎呢,那就打開vs敲代碼吧,先去弄個基礎點的東西,不過個人覺得人為計算這個沒有多大實際意義,當然這個跟我菜鳥身份有關,對程式設計感興趣的同學可以看看,這次就以一種非正規的編寫習慣上吧,計算公式部分和邏輯就不給出注釋了

static void Main(string[] args)
        {
            Input();
        }
        /// <summary>
        /// 錄入資料
        /// </summary>
        private static void Input()
        {
            Console.WriteLine("請選擇一種轉換方式:");
            Console.WriteLine("A:十進制轉二進制");
            Console.WriteLine("B:二進制轉十進制");
            Console.WriteLine("C:十進制轉十六進制");
            Console.WriteLine("D:十六進制轉十進制");
            string selectType = Console.ReadLine().Trim().ToUpper();
            Console.WriteLine("請錄入要轉換的資料:");
            string input = Console.ReadLine();
            string result = "";
            switch (selectType)
            {
                case "A": result = Convert10ToOther(input, 2); break;
                case "B": result = ConvertOtherTo10(input, 2); break;
                case "C": result = Convert10ToOther(input, 16); break;
                case "D": result = ConvertOtherTo10(input, 16); break;
                default: break;
            }
            if (String.IsNullOrEmpty(result)) { Input(); return; }
            Console.WriteLine("結果為:{0}", result);
            Console.WriteLine("按'Y'繼續,任意鍵退出:");
            if (Console.ReadLine().Trim().ToUpper() == "Y")
            {
                Input();
            }
        }

        /// <summary>
        /// 将十進制轉成其他進制
        /// </summary>
        /// <param name="input"></param>
        /// <param name="baseNumber"></param>
        /// <returns></returns>
        private static string Convert10ToOther(string input, int baseNumber)
        {
            int source;
            if (!int.TryParse(input, out source))
            {
                Console.WriteLine("輸入格式有誤,請重試:");
                return "";
            }
            List<int> target = new List<int>();
            while (source > 0)
            {
                target.Add(source % baseNumber);
                source = Convert.ToInt32(Math.Floor(source / Convert.ToDecimal(baseNumber)));
            }
            target.Reverse();
            StringBuilder sbResult = new StringBuilder();
            foreach (int item in target)
            {
                sbResult.Append(Get16NumEng(item));
            }
            return sbResult.ToString();
        }
        /// <summary>
        /// 将其他進制轉成十進制
        /// </summary>
        /// <param name="input"></param>
        /// <param name="baseNumber"></param>
        /// <returns></returns>
        private static string ConvertOtherTo10(string input, int baseNumber)
        {
            char[] source = input.ToCharArray();
            List<string> liSour = new List<string>();
            for (int i = source.Length - 1; i > -1; i--)
            {
                liSour.Add(source[i].ToString());
            }
            int result = 0;
            for (int i = 0; i < liSour.Count; i++)
            {
                result += Convert.ToInt32(Convert.ToInt32(Get16NumInt(liSour[i])) * Math.Pow(baseNumber, i));
            }
            return result.ToString();
        }
        /// <summary>
        /// 十六進制英文對應的數字
        /// </summary>
        /// <param name="sou"></param>
        /// <returns></returns>
        private static string Get16NumInt(string sou)
        {
            string result;
            switch (sou.ToUpper())
            {
                case "A": result = "10"; break;
                case "B": result = "11"; break;
                case "C": result = "12"; break;
                case "D": result = "13"; break;
                case "E": result = "14"; break;
                case "F": result = "15"; break;
                default: result = sou.ToString(); break;
            }
            return result;
        }
        /// <summary>
        /// 十六進制數字對應的英文
        /// </summary>
        /// <param name="sou"></param>
        /// <returns></returns>
        private static string Get16NumEng(int sou)
        {
            string result;
            switch (sou)
            {
                case 10: result = "A"; break;
                case 11: result = "B"; break;
                case 12: result = "C"; break;
                case 13: result = "D"; break;
                case 14: result = "E"; break;
                case 15: result = "F"; break;
                default: result = sou.ToString(); break;
            }
            return result;
        }