-
Notifications
You must be signed in to change notification settings - Fork 1
/
150120007_150121019_150121029.c
204 lines (178 loc) · 6.32 KB
/
150120007_150121019_150121029.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
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
/**
* Mustafa Emir Uyar - 150120007
* Umut Özil - 150121019
* Ege Keklikçi - 150121029
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <math.h>
#define min(X, Y) (((X) < (Y)) ? (X) : (Y))
void readFile(char fileName[], char byteOrder, char type[], int dataSize);
void hexToBin(char hex[][3], char bin[], int dataSize);
void binaryToIEEE(char* binaryNumber, int dataTypeSize);
void bitToIntSigned(char bits[], int dataSize);
void bitToIntUnsigned(char bits[], int dataSize);
FILE* output;
int main(int argc, char* argv[]) {
// create a file for output in writing mode
output = fopen("output.txt", "w");
// take the inputs as command-line arguments
if (argc == 5)
readFile(argv[1], argv[2][0], argv[3], (argv[4][0]-'0'));
else
printf("Inputs are missing");
}
/* This function reads the input file according to byte ordering and dataSize of the data */
void readFile(char fileName[], char byteOrder, char dataType[], int dataSize) {
FILE* input = fopen(fileName, "r");
char hex[dataSize][3];
char temp[3];
int count = 1;
while(fscanf(input, "%s", temp) == 1) {
if ((count - 1) % (12 / dataSize) == 0 && count != 1)
fprintf(output, "\n");
// if the byte ordering type is BigEndian read data from beginning
if (byteOrder == 'b') {
strcpy(hex[0], temp);
for (int i = 1; i < dataSize; i++)
fscanf(input, "%s", hex[i]);
}
// if the byte ordering type is LittleEndian read data from the end
else if (byteOrder == 'l') {
strcpy(hex[dataSize - 1], temp);
for (int i = dataSize - 2; i >= 0; i--)
fscanf(input, "%s", hex[i]);
}
char bin[8*dataSize];
hexToBin(hex, bin, dataSize);
// call function acording to given dataType
if (strcmp(dataType, "float") == 0)
binaryToIEEE(bin, dataSize);
else if (strcmp(dataType, "int") == 0)
bitToIntSigned(bin, dataSize);
else if (strcmp(dataType, "unsigned") == 0)
bitToIntUnsigned(bin, dataSize);
if (count % (12 / dataSize) != 0)
fprintf(output, " ");
count++;
}
}
/* This function converts hexadecimal data to binary */
void hexToBin(char hex[][3], char bin[], int dataSize) {
// convert hexadecimal data to binary
for (int i = 0; i < dataSize; i++) {
for (int j = 0; j < 2; j++) {
int dec;
if (hex[i][j] <= '9')
dec = hex[i][j] - '0'; // for 0-9
else
dec = hex[i][j] - 'a' + 10; // for a-f
for (int k = 0; k < 4; k++) {
bin[3+(i*8)+(j*4)-k] = (dec % 2) + '0'; // int to char
dec /= 2;
}
}
}
}
/* This function converts given binary number to float using IEEE-like format */
void binaryToIEEE(char* binaryNumber, int dataTypeSize) {
//Calculate the boundaries and bias
int expDigitSize = 2 + dataTypeSize*2;
int fractionLastIndex = min(dataTypeSize*8,expDigitSize+14);
int exponentValue = 0;
float fractionValue = 1 ;
int bias = pow(2,expDigitSize-1)-1;
int sign = binaryNumber[0]-'0';
//Calculating the exponent
for (int i = expDigitSize+1; i > 0 ; i--)
exponentValue += (binaryNumber[i]-'0')*pow(2,expDigitSize-i);
//If the exponent is 0 the number will be Denormalized and fraction will start with 0.xxx
if (exponentValue == 0)
fractionValue = 0;
//Rounding the mantissa
int roundedDigit = expDigitSize+14;
int roundToEven = 1;
if (dataTypeSize>2 && binaryNumber[roundedDigit--]== '1') {
//Round Up
for (int i = roundedDigit+2; i < dataTypeSize*8; i++)
{
if (binaryNumber[i] == '1')
{
roundToEven = 0;
while(binaryNumber[roundedDigit]!='0')
binaryNumber[roundedDigit--]='0';
binaryNumber[roundedDigit]='1';
break;
}
}
//Round to Even
if (roundToEven && binaryNumber[roundedDigit] == '1') {
while(binaryNumber[roundedDigit]!='0')
binaryNumber[roundedDigit--]='0';
binaryNumber[roundedDigit]='1';
}
}
//Calculating the mantissa
for (int i = expDigitSize+1; i < fractionLastIndex; i++) {
fractionValue+= (binaryNumber[i]-'0')*pow(2,expDigitSize-i);
}
//Classifying and calculating the result.
if (exponentValue == pow(2,expDigitSize)-1) { //Special values
if (fractionValue == 0 || fractionValue == 1) {
if (sign==0)
fprintf(output, "∞");
else
fprintf(output, "-∞");
}
else
fprintf(output, "NaN");
}
else if (exponentValue == 0) { // Denormalized values
double result = pow(-1, sign)*fractionValue/pow(2,bias-1);
if (result==0)
fprintf(output, "%.0f", result);
else
fprintf(output, "%g", result);
}
else { //Normalized values
float result = pow(-1, sign)*fractionValue*pow(2,exponentValue-bias);
fprintf(output, "%g", result);
}
}
/* This function converts given binary number to integer using 2's compelement representation */
void bitToIntSigned(char bits[], int dataSize) {
int number = 0, power = 1;
if (bits[0]=='0') {
for (int i=8*dataSize-1; i>=1 ; i--) {
if (bits[i]=='1') {
number += power;
}
power *= 2;
}
}
// for calculating negative numbers first invert the digits and calculate the number
else if(bits[0]=='1') {
for (int i=8*dataSize-1; i >= 1 ; i--) {
if (bits[i]=='0') {
number += power;
}
power *= 2;
}
// then add one to the number and multiply by -1
number += 1;
number *= -1;
}
fprintf(output, "%d", number);
}
/* This function converts given binary number to unsigned integer */
void bitToIntUnsigned(char bits[], int dataSize) {
unsigned int number = 0, power = 1;
for (int i=8*dataSize-1; i>=0 ; i--) {
if (bits[i]=='1') {
number += power;
}
power *= 2;
}
fprintf(output, "%u", number);
}