Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- class HexToDecimal
- {
- static void Main()
- {
- string hex = Console.ReadLine();
- long result = 0;
- int bufferNum = 0;
- bool check = false;
- for (int cnt = 1; cnt <= hex.Length; cnt++)
- {
- result *= 16;
- check = int.TryParse(hex[cnt - 1].ToString(), out bufferNum);
- if(!check)
- {
- bufferNum = getDecimal(hex[cnt-1]);
- }
- result += bufferNum;
- }
- Console.WriteLine(result);
- }
- private static int getDecimal(char p)
- {
- int value = 0;
- switch (p)
- {
- case 'A':
- value = 10;
- break;
- case 'B':
- value = 11;
- break;
- case 'C':
- value = 12;
- break;
- case 'D':
- value = 13;
- break;
- case 'E':
- value = 14;
- break;
- case 'F':
- value = 15;
- break;
- }
- return value;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment