-
Notifications
You must be signed in to change notification settings - Fork 765
/
Copy pathOctal to Hexadecimal in C
97 lines (95 loc) · 2.45 KB
/
Octal to Hexadecimal in 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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <stdio.h>
#include<string.h>
int main()
{
int octaltobinary[]={0,1,10,11,100,101,110,111};
char hexadecimal[10];
char hex[10];
long int binary=0;
int octal;
int rem=0;
int position=1;
int len=0;
int k=0;
printf("Enter a octal number");
scanf("%d",&octal);
// Converting octal number into a binary number.
while(octal!=0)
{
rem=octal%10;
binary=octaltobinary[rem]*position+binary;
octal=octal/10;
position=position*1000;
}
printf("The binary number is : %ld",binary);
// Converting binary number into a hexadecimal number.
while(binary > 0)
{
rem = binary % 10000;
switch(rem)
{
case 0:
strcat(hexadecimal, "0");
break;
case 1:
strcat(hexadecimal, "1");
break;
case 10:
strcat(hexadecimal, "2");
break;
case 11:
strcat(hexadecimal, "3");
break;
case 100:
strcat(hexadecimal, "4");
break;
case 101:
strcat(hexadecimal, "5");
break;
case 110:
strcat(hexadecimal, "6");
break;
case 111:
strcat(hexadecimal, "7");
break;
case 1000:
strcat(hexadecimal, "8");
break;
case 1001:
strcat(hexadecimal, "9");
break;
case 1010:
strcat(hexadecimal, "A");
break;
case 1011:
strcat(hexadecimal, "B");
break;
case 1100:
strcat(hexadecimal, "C");
break;
case 1101:
strcat(hexadecimal, "D");
break;
case 1110:
strcat(hexadecimal, "E");
break;
case 1111:
strcat(hexadecimal, "F");
break;
}
len=len+1;
binary /= 10000;
}
for(int i=len-1;i>=0;i--)
{
hex[k]=hexadecimal[i];
k++;
}
hex[len]='\0';
printf("\nThe hexadecimal number is :");
for(int i=0; hex[i]!='\0';i++)
{
printf("%c",hex[i]);
}
return 0;
}