-
Notifications
You must be signed in to change notification settings - Fork 6
/
DecimalToHexadecimalViceVersa.c
83 lines (80 loc) · 1.82 KB
/
DecimalToHexadecimalViceVersa.c
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
#include <stdio.h>
#include <math.h>
#include <string.h>
void decimal_hex(int n, char hex[]);
int hex_decimal(char hex[]);
int main()
{
char hex[20];
int n,c;
printf("**----Program to Convert Decimal and Hexadeciaml Vice Versa----***\n\n");
printf("Choose Your Choice: \n");
printf("1.Decimal to Hexadecimal:\n");
printf("2.Hexadecimal to Decimal:\n\n");
scanf("%d",&c);
if (c==1)
{
printf("Enter decimal number: ");
scanf("%d",&n);
decimal_hex(n,hex);
printf("Hexadecimal number: %s",hex);
}
if (c==2)
{
printf("Enter hexadecimal number: ");
scanf("%s",hex);
printf("Decimal number: %d",hex_decimal(hex));
}
return 0;
}
void decimal_hex(int n, char hex[])
{
int i=0,rem;
while (n!=0)
{
rem=n%16;
switch(rem)
{
case 10:
hex[i]='A';
break;
case 11:
hex[i]='B';
break;
case 12:
hex[i]='C';
break;
case 13:
hex[i]='D';
break;
case 14:
hex[i]='E';
break;
case 15:
hex[i]='F';
break;
default:
hex[i]=rem+'0';
break;
}
++i;
n/=16;
}
hex[i]='\0';
strrev(hex);
}
int hex_decimal(char hex[])
{
int i, length, sum=0;
for(length=0; hex[length]!='\0'; ++length);
for(i=0; hex[i]!='\0'; ++i, --length)
{
if(hex[i]>='0' && hex[i]<='9')
sum+=(hex[i]-'0')*pow(16,length-1);
if(hex[i]>='A' && hex[i]<='F')
sum+=(hex[i]-55)*pow(16,length-1);
if(hex[i]>='a' && hex[i]<='f')
sum+=(hex[i]-87)*pow(16,length-1);
}
return sum;
}