Advertisement
remote87

Decimal To Hexa loops only

Sep 9th, 2015
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.51 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6.  
  7. namespace _16.DecimalToHexadecimal
  8. {
  9.     class DecimalToHexadecimal
  10.     {
  11.         static void Main()
  12.         {
  13.             Console.Write("Enter a number to convert in hexadecimal: ");
  14.             long dec = long.Parse(Console.ReadLine());
  15.             long result = 0;
  16.             string hexResult = string.Empty;
  17.             for (long i = dec; i >= 1 ; i /= 16)
  18.             {
  19.                 result = i % 16;
  20.                 string hex = string.Empty;
  21.                
  22.                 switch (result)
  23.                 {
  24.                     case 10:
  25.                         hex = "A" + hex;
  26.                         break;
  27.                     case 11:
  28.                         hex = "B" + hex;
  29.                         break;
  30.                     case 12:
  31.                         hex = "C" + hex;
  32.                         break;
  33.                     case 13:
  34.                         hex = "D" + hex;
  35.                         break;
  36.                     case 14:
  37.                         hex = "E" + hex;
  38.                         break;
  39.                     case 15:
  40.                         hex = "F" + hex;
  41.                         break;
  42.                     default:
  43.                         hex = hex + result;
  44.                         break;
  45.                 }
  46.                 hexResult += hex;
  47.             }
  48.             Console.WriteLine(hexResult.Reverse().ToArray());
  49.         }
  50.     }
  51. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement