Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- using System;
- // 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.
- class Pr16DecimalToHexadecimalNumber
- {
- static void Main()
- {
- long decNum = long.Parse(Console.ReadLine());
- long tenPower = 1;
- string hexNum = "";
- while (decNum > 0)
- {
- long hexDigit = (long) decNum % 16;
- if (decNum % 16 > 9)
- {
- hexNum += (char) (hexDigit + 55); // представяне на шестнайсетичните цифри след 9, без ползване на switch оператор
- }
- else
- {
- hexNum += hexDigit.ToString();
- }
- decNum >>= 4; // шифтването с 4 бита на дясно е аналогично да деление на 2^4 (16), но е много по-лека операция
- tenPower *= 10;
- }
- string result = "";
- for (int i = hexNum.Length - 1; i >= 0; i--)
- {
- Console.Write(hexNum[i]);
- }
- Console.WriteLine();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement