Advertisement
Guest User

Hexadecimal To Decimal

a guest
Jun 21st, 2015
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.96 KB | None | 0 0
  1. using System;
  2.  
  3. class HexademicalToDecimal
  4. {
  5.     static void Main()
  6.     {
  7.         string hex = Console.ReadLine();
  8.         long resultindecimal = 0;
  9.         int element = 0;
  10.  
  11.         for (int i = hex.Length - 1; i >= 0; i--)
  12.         {
  13.             if (hex[i] > '9')
  14.             {
  15.                 element = hex[i] - '7';
  16.                 // example: 'A' = 65 (ASCII code); '7' = 55; hence 'A' - '7' = 10,
  17.                 // which is exactly the decimal representation of the digit A
  18.             }
  19.             else
  20.             {
  21.                 element = hex[i] - '0';
  22.             }
  23.  
  24.             resultindecimal += element * PowerOf(hex.Length - (i + 1));
  25.  
  26.         }
  27.         Console.WriteLine(resultindecimal);
  28.  
  29.  
  30.     }
  31.     static long PowerOf(int grade)
  32.     {
  33.         long result = 1;
  34.         int poweredNum = 16;
  35.         for (int i = 1; i <= grade; i++)
  36.         {
  37.  
  38.             result *= poweredNum;
  39.         }
  40.  
  41.         return result;
  42.     }
  43. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement