2

I am trying to convert a decimal number (329.39062) to binary (exponent, mentissa). I keep getting segmentation fault. on running the gdb test, It shows me feof. I have changed alot but it keeps showing me the segmentation fault at the same point. Where am i going wrong? Thank you for all the help.

#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;
}
4

1 に答える 1