Guest User

Untitled

a guest
May 21st, 2018
141
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.20 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5.  
  6. namespace P3.ConvertDecimalToHexadecimal
  7. {
  8.     public class Program
  9.     {
  10.         private static string hexNumbers = "0123456789ABCDEF";
  11.         static void Main()
  12.         {          
  13.             Console.Write("Enter the number -> ");
  14.             int numberInDec = int.Parse(Console.ReadLine());            
  15.             Console.WriteLine("The number in hex -> " + ConvertNumberFromDecimalToHexadecimal(numberInDec));
  16.         }
  17.  
  18.         private static string ConvertNumberFromDecimalToHexadecimal(int pNumberInDec)
  19.         {
  20.             LinkedList<char> invertedNumberInHex = new LinkedList<char>();
  21.             StringBuilder numberInHex = new StringBuilder();          
  22.             while (pNumberInDec > 0)
  23.             {
  24.                 invertedNumberInHex.AddLast(hexNumbers[pNumberInDec % 16]);
  25.                 pNumberInDec >>= 4;
  26.             }
  27.             while (invertedNumberInHex.Count > 0)
  28.             {
  29.                 numberInHex.Append(invertedNumberInHex.Last.Value);
  30.                 invertedNumberInHex.RemoveLast();
  31.             }
  32.             return numberInHex.ToString();
  33.         }
  34.     }
  35. }
Add Comment
Please, Sign In to add comment