-
Notifications
You must be signed in to change notification settings - Fork 4
/
16.Decimal To Hexadecimal Number
52 lines (50 loc) · 1.34 KB
/
16.Decimal To Hexadecimal Number
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
using System;
class DecimalToHexadecimalNumber
{
static void Main()
{
Console.WriteLine("Enter an integer:");
long decimalNum = long.Parse(Console.ReadLine());
string hexNum = string.Empty;
string character = string.Empty;
long result = 0;
long quotient = 1;
while (quotient != 0)
{
result = decimalNum % 16;
quotient = decimalNum / 16;
character=DeterminationNumber(result);
hexNum += character;
decimalNum = quotient;
}
char[] valueChars = hexNum.ToCharArray();
Array.Reverse(valueChars);
for (int i = 0; i < hexNum.Length; i++)
{
Console.Write("{0}", valueChars[i]);
}
Console.WriteLine();
}
private static string DeterminationNumber(long number)
{
string str = string.Empty;
switch (number)
{
case 10: str = "A";
break;
case 11: str = "B";
break;
case 12: str = "C";
break;
case 13: str = "D";
break;
case 14: str = "E";
break;
case 15: str = "F";
break;
default: str = number.ToString();
break;
}
return str;
}
}