-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtemp.c
More file actions
131 lines (127 loc) · 2.33 KB
/
Copy pathtemp.c
File metadata and controls
131 lines (127 loc) · 2.33 KB
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int pow_(int a,int b)
{
int f = 1;
while(b--)
{
f *= a;
}
return f;
}
float to_decimal(char *hex)
{
float num = 0;
int p = 0;
// partition
char float_part[100];
char hex_num[100];
int i;
for(i=0;hex[i]!='.' && i<strlen(hex);i++)
{
hex_num[i]=hex[i];
}
hex_num[i]='\0';
if( i <strlen(hex))
{
//float part is there
i++;
int j = 1;
for(i;i <strlen(hex);i++,j++)
{
if(hex[i]>=48 && hex[i]<=57)
{
num += ((float)(hex[i]-48)/(float)(pow_(16,j)));
}
else
{
num += ((float)(hex[i]-'a'+10)/(float)(pow_(16,j)));
}
}
}
// printf("\nconverting %s \n",hex_num);
for(int i=strlen(hex_num)-1;i>=0;i--,p++)
{
if(isdigit(hex_num[i]))
{
num += (hex_num[i]-'0')*pow_(16,p);
}
else
{
num += (hex_num[i]-'a'+10)*pow_(16,p);
}
}
// printf("%d converted\n",num);
return num;
}
void to_hex(float num_h,char* num_hex)
{
//printf("\nconverting to hex : %d\n",num);
int num = (int)(num_h);
num_h = num_h - num;
printf("\n%f num_h %d num\n",num_h,num);
char hex_num[100];
int i=0;
while(num)
{
int rem = num%16;
num /= 16;
if(rem<10)
{
hex_num[i]=rem+48;
}
else
{
hex_num[i] = rem-10 + 'a';
}
i++;
}
hex_num[i]='\0';
// reverse
char final_hex[100];
for(int i=0;i<strlen(hex_num);i++)
{
final_hex[strlen(hex_num)-1-i] = hex_num[i];
}
final_hex[strlen(hex_num)]='\0';
i = strlen(hex_num) + 1;
//printf("\ndecimal to hex : %s\n",final_hex);
if(num_hex)
{
strcat(final_hex,".");
int count = 0;
while(num_h != 16 && count<= 20)
{
num_h = num_h * 16;
int first_part = num_h;
printf("%d is first part\n",first_part);
if(num_h > 0 && num_h != 16)
{
num_h = num_h - first_part;
printf("after removing first part : %f and first part %d\n",num_h,first_part);
}
printf("%f is num_h \n",num_h);
if(first_part <= 9)
{
final_hex[i++] = first_part+48;
}
else
{
final_hex[i++] = first_part - 10 + 'a';
}
count++;
}
final_hex[i]='\0';
}
strcpy(num_hex,final_hex);
}
void main()
{
char num[100];
float f;
printf("\nEnter decimal number : ");
scanf("%f",&f);
to_hex(f,num);
printf("\nHexadecimal equivalent is : %s\n",num);
}