Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- //Write a program to convert hexadecimal numbers to their decimal representation
- class Program
- {
- static void Main()
- {
- Console.WriteLine("Enter hex number : ");
- string convert = Console.ReadLine();
- convert = convert.ToLower();
- int result = 0;
- for (int i = convert.Length - 1, reverse = 0; i >= 0 ; i--,reverse ++)
- {
- if (convert[i] <= '9')
- {
- result += (convert[i] - '0') * (int)Math.Pow(16,reverse) ;
- }
- else if (convert[i] <= 'f')
- {
- result += (convert[i] - 'a' + 10) * (int)Math.Pow(16, reverse);
- }
- }
- Console.WriteLine("Converted number :");
- Console.WriteLine(result);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment