Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- using System.Collections.Generic;
- /// <summary>
- /// Using loops write a program that converts an integer number
- /// to its hexadecimal representation. The input is entered as
- /// long. The output should be a variable of type string. Do not
- /// use the built-in .NET functionality.
- /// </summary>
- public class DecToHex
- {
- public static void ConvertToHex(long input)
- {
- string output = string.Empty;
- long result = 0;
- List<string> inputHex = new List<string>();
- while (input >= 1)
- {
- result = input % 16;
- input /= 16;
- if (result < 10)
- {
- inputHex.Add(Convert.ToString(result));
- }
- else if (result == 10)
- {
- inputHex.Add("A");
- }
- else if (result == 11)
- {
- inputHex.Add("B");
- }
- else if (result == 12)
- {
- inputHex.Add("C");
- }
- else if (result == 13)
- {
- inputHex.Add("D");
- }
- else if (result == 14)
- {
- inputHex.Add("E");
- }
- else if (result == 15)
- {
- inputHex.Add("F");
- }
- }
- inputHex.Reverse();
- foreach (var num in inputHex)
- {
- output += num;
- }
- Console.WriteLine(output);
- }
- static void Main()
- {
- long input = long.Parse(Console.ReadLine());
- ConvertToHex(input);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement