Panglot

Decimal To hexadecimal

Apr 8th, 2014
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.92 KB | None | 0 0
  1. using System;
  2. class DecToHex
  3. {
  4.     static void Main()
  5.     {
  6.         Console.Write("Enter decimal number: ");
  7.         long n = long.Parse(Console.ReadLine());
  8.  
  9.  
  10.         string result = "";
  11.  
  12.  
  13.         for (; n != 0; n /= 16)
  14.         {
  15.             if (n % 16 == 15)
  16.                 result += "F";
  17.             else if (n % 16 == 14)
  18.                 result += "E";
  19.             else if (n % 16 == 13)
  20.                 result += "D";
  21.             else if (n % 16 == 12)
  22.                 result += "C";
  23.             else if (n % 16 == 11)
  24.                 result += "B";
  25.             else if (n % 16 == 10)
  26.                 result += "A";
  27.             else
  28.                 result += (char)(48 + n % 16);
  29.        
  30.         }
  31.  
  32.         char[] forReverse = result.ToCharArray();
  33.         Array.Reverse(forReverse);
  34.         result = new string(forReverse);
  35.  
  36.         Console.WriteLine("The number in hexadecimal: "+result);
  37.  
  38.  
  39.     }
  40. }
Advertisement
Add Comment
Please, Sign In to add comment