Advertisement
Teodor92

DecimalToHexadecimal

Jan 15th, 2013
590
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System;
  2. using System.Text;
  3. using System.Linq;
  4.  
  5. class DecimalToHexadecimal
  6. {
  7.     static void Main()
  8.     {
  9.         int decNum = int.Parse(Console.ReadLine());
  10.         StringBuilder hexNum = new StringBuilder();
  11.         while (decNum > 0)
  12.         {
  13.             switch (decNum % 16)
  14.             {
  15.                 case 10:
  16.                     hexNum.Append('A');
  17.                     break;
  18.                 case 11:
  19.                     hexNum.Append('B');
  20.                     break;
  21.                 case 12:
  22.                     hexNum.Append('C');
  23.                     break;
  24.                 case 13:
  25.                     hexNum.Append('D');
  26.                     break;
  27.                 case 14:
  28.                     hexNum.Append('E');
  29.                     break;
  30.                 case 15:
  31.                     hexNum.Append('F');
  32.                     break;
  33.                 default:
  34.                     hexNum.Append(decNum % 16);
  35.                     break;
  36.             }
  37.             decNum = decNum / 16;
  38.         }
  39.         string endNum = hexNum.ToString();
  40.         for (int i = endNum.Length - 1; i > -1; i--)
  41.         {
  42.             Console.Write(endNum[i]);
  43.         }
  44.         Console.WriteLine();
  45.     }
  46. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement