BorisSimeonov

Hexidecimal Input To Decimal

Dec 8th, 2014
220
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.14 KB | None | 0 0
  1. using System;
  2.  
  3. class HexToDecimal
  4. {
  5.     static void Main()
  6.     {
  7.         string hex = Console.ReadLine();
  8.         long result = 0;
  9.         int bufferNum = 0;
  10.         bool check = false;
  11.  
  12.         for (int cnt = 1; cnt <= hex.Length; cnt++)
  13.         {
  14.             result *= 16;
  15.             check = int.TryParse(hex[cnt - 1].ToString(), out bufferNum);
  16.             if(!check)
  17.             {
  18.                 bufferNum = getDecimal(hex[cnt-1]);
  19.             }
  20.             result += bufferNum;
  21.         }
  22.         Console.WriteLine(result);
  23.     }
  24.  
  25.     private static int getDecimal(char p)
  26.     {
  27.         int value = 0;
  28.         switch (p)
  29.         {
  30.             case 'A':
  31.                 value = 10;
  32.                 break;
  33.             case 'B':
  34.                 value = 11;
  35.                 break;
  36.             case 'C':
  37.                 value = 12;
  38.                 break;
  39.             case 'D':
  40.                 value = 13;
  41.                 break;
  42.             case 'E':
  43.                 value = 14;
  44.                 break;
  45.             case 'F':
  46.                 value = 15;
  47.                 break;
  48.         }
  49.         return value;
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment