Advertisement
Guest User

Pr16DecimalToHexadecimalNumber

a guest
Jul 13th, 2015
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.25 KB | None | 0 0
  1. using System;
  2.  
  3. // Using loops write a program that converts an integer number to its hexadecimal representation.
  4. // The input is entered as long. The output should be a variable of type string.
  5. // Do not use the built-in .NET functionality.
  6.  
  7. class Pr16DecimalToHexadecimalNumber
  8. {
  9.     static void Main()
  10.     {
  11.         long decNum = long.Parse(Console.ReadLine());
  12.         long tenPower = 1;
  13.         string hexNum = "";
  14.         while (decNum > 0)
  15.         {
  16.             long hexDigit = (long) decNum % 16;
  17.             if (decNum % 16 > 9)
  18.             {
  19.                 hexNum += (char) (hexDigit + 55); // представяне на шестнайсетичните цифри след 9, без ползване на switch оператор
  20.             }
  21.             else
  22.             {
  23.                 hexNum += hexDigit.ToString();
  24.             }
  25.             decNum >>= 4; // шифтването с 4 бита на дясно е аналогично да деление на 2^4 (16), но е много по-лека операция
  26.             tenPower *= 10;
  27.         }
  28.         string result = "";
  29.         for (int i = hexNum.Length - 1; i >= 0; i--)
  30.         {
  31.             Console.Write(hexNum[i]);
  32.         }
  33.         Console.WriteLine();
  34.     }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement