Advertisement
Sekklow

DecToHex

Aug 18th, 2014
226
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.63 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. /// <summary>
  5. /// Using loops write a program that converts an integer number
  6. /// to its hexadecimal representation. The input is entered as
  7. /// long. The output should be a variable of type string. Do not
  8. /// use the built-in .NET functionality.
  9. /// </summary>
  10. public class DecToHex
  11. {
  12.     public static void ConvertToHex(long input)
  13.     {
  14.         string output = string.Empty;
  15.         long result = 0;
  16.         List<string> inputHex = new List<string>();
  17.         while (input >= 1)
  18.         {
  19.             result = input % 16;
  20.             input /= 16;
  21.             if (result < 10)
  22.             {
  23.                 inputHex.Add(Convert.ToString(result));
  24.             }
  25.             else if (result == 10)
  26.             {
  27.                 inputHex.Add("A");
  28.  
  29.             }
  30.             else if (result == 11)
  31.             {
  32.                 inputHex.Add("B");
  33.  
  34.             }
  35.             else if (result == 12)
  36.             {
  37.                 inputHex.Add("C");
  38.  
  39.             }
  40.             else if (result == 13)
  41.             {
  42.                 inputHex.Add("D");
  43.  
  44.             }
  45.             else if (result == 14)
  46.             {
  47.                 inputHex.Add("E");
  48.  
  49.             }
  50.             else if (result == 15)
  51.             {
  52.                 inputHex.Add("F");
  53.  
  54.             }
  55.         }
  56.  
  57.         inputHex.Reverse();
  58.         foreach (var num in inputHex)
  59.         {
  60.             output += num;
  61.         }
  62.  
  63.         Console.WriteLine(output);
  64.     }
  65.  
  66.     static void Main()
  67.     {
  68.         long input = long.Parse(Console.ReadLine());
  69.  
  70.         ConvertToHex(input);
  71.     }
  72. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement