网站建议:179001057@qq.com

将十六进制数转换成十进制数

技术2022-05-11  0

class Program { void ConvertToHex() { double n = 0; Console.Write("输入十六进制数:"); string s = Console.ReadLine().ToLower(); if (s != "q") { byte[] b = Encoding.Default.GetBytes(s); for (int i = 0; i < b.Length; i++) { if (b[i] > 96&&b[i]<103) b[i] -= 87; else if(b[i]>47&&b[i]<58) b[i] -= 48; else Console.WriteLine("输入不合法"); } for (int i = 0; i < s.Length; i++) { n += b[s.Length-1-i] * Math.Pow(16, i); } Console.WriteLine("{0}的十进制数:{1}",s ,n); Console.WriteLine(); ConvertToHex(); } } static void Main(string[] args) { Program c = new Program(); Console.WriteLine("输入“Q”退出:"); c.ConvertToHex(); } }

 

运行结果:


最新回复(0)