Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- using System.Linq;
- using System.Text;
- namespace P3.ConvertDecimalToHexadecimal
- {
- public class Program
- {
- private static string hexNumbers = "0123456789ABCDEF";
- static void Main()
- {
- Console.Write("Enter the number -> ");
- int numberInDec = int.Parse(Console.ReadLine());
- Console.WriteLine("The number in hex -> " + ConvertNumberFromDecimalToHexadecimal(numberInDec));
- }
- private static string ConvertNumberFromDecimalToHexadecimal(int pNumberInDec)
- {
- LinkedList<char> invertedNumberInHex = new LinkedList<char>();
- StringBuilder numberInHex = new StringBuilder();
- while (pNumberInDec > 0)
- {
- invertedNumberInHex.AddLast(hexNumbers[pNumberInDec % 16]);
- pNumberInDec >>= 4;
- }
- while (invertedNumberInHex.Count > 0)
- {
- numberInHex.Append(invertedNumberInHex.Last.Value);
- invertedNumberInHex.RemoveLast();
- }
- return numberInHex.ToString();
- }
- }
- }
Add Comment
Please, Sign In to add comment