Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class HexademicalToDecimal
- {
- static void Main()
- {
- string hex = Console.ReadLine();
- long resultindecimal = 0;
- int element = 0;
- for (int i = hex.Length - 1; i >= 0; i--)
- {
- if (hex[i] > '9')
- {
- element = hex[i] - '7';
- // example: 'A' = 65 (ASCII code); '7' = 55; hence 'A' - '7' = 10,
- // which is exactly the decimal representation of the digit A
- }
- else
- {
- element = hex[i] - '0';
- }
- resultindecimal += element * PowerOf(hex.Length - (i + 1));
- }
- Console.WriteLine(resultindecimal);
- }
- static long PowerOf(int grade)
- {
- long result = 1;
- int poweredNum = 16;
- for (int i = 1; i <= grade; i++)
- {
- result *= poweredNum;
- }
- return result;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement