Advertisement
hristo_bratanov

Convert decimal to hexadecimal

Jan 17th, 2013
206
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.24 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3.  
  4. class DecimalToHex
  5. {
  6.     static void Main()
  7.     {
  8.         string input = Console.ReadLine();
  9.         PrintReversed(ConvertDectoHex(input));
  10.     }
  11.     static List<char> ConvertDectoHex(string number)
  12.     {
  13.         int numInt = Int32.Parse(number);
  14.         int index = -1;
  15.         List<char> result = new List<char>();
  16.         while (numInt >= 1)
  17.         {
  18.             result.Add('0');
  19.             int reminder = numInt % 16;
  20.             index++;
  21.             switch (reminder)
  22.             {
  23.                 case 10: result[index] = 'A'; break;
  24.                 case 11: result[index] = 'B'; break;
  25.                 case 12: result[index] = 'C'; break;
  26.                 case 13: result[index] = 'D'; break;
  27.                 case 14: result[index] = 'E'; break;
  28.                 case 15: result[index] = 'F'; break;
  29.                 default: result[index] = (char)(reminder+48); break;
  30.             }
  31.             numInt = numInt / 16;
  32.         }
  33.         return result;
  34.     }
  35.  
  36.     static void PrintReversed(List<char> arr)
  37.     {
  38.        
  39.         for (int i = arr.Count-1; i >= 0; i--)
  40.         {
  41.             Console.Write((char)(arr[i]));
  42.         }
  43.         Console.WriteLine();
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement