我正在尝试将十进制数(329.39062)转换为二进制(指数,mentissa)。我不断收到分段错误。在运行 gdb 测试时,它向我展示了 feof。我已经改变了很多,但它一直在同一点向我显示分段错误。我哪里错了?谢谢大家的帮助。
#include <stdio.h>
#include <stdlib.h>
char* valueToConvert(int value);
int main(int argc, char *argv[])
{
FILE* input;
FILE* output;
input = fopen(argv[1],"r");
output = fopen(argv[2],"w");
float value;
unsigned char *charValue = (unsigned char *) &value;
int exponentValue;
long mantissaValue;
while(!feof(input))
{
fread(&charValue, sizeof(float),1, input);
exponentValue = ((charValue[0] & 0x7F) << 1)|((charValue[1] & 0x80) >> 7);
mantissaValue = ((charValue[1] & 0x7F) << 8)|((charValue[2] & 0xFF) <<8) | (charValue[3] & 0xFF);
fprintf(output,"%d %s %s\n",(charValue[0] & 0x80 >> 7),valueToConvert(exponentValue - 127),valueToConvert(mantissaValue));
}
}
char* valueToConvert(int value)
{
int counter = 0;
char* conversion = calloc(32,sizeof(int));
while(value>0)
{
if((value%2 == 1) && (value%2 != 0))
{
conversion[31 - counter++] = '1';
}
if((value%2 == 0) && (value%2 != 1))
{
conversion[31 - counter++] = '0';
}
value = value/2;
}
return conversion;
}