3

I'm trying to read the following binary (01100001) from a file and convert it to ascii code (97), but when using fread i'm getting a very big numbers. the file "c:/input.txt" contain only the following line -01100001 printf of the array values print big numbers, such as 825241648

My code:

int main()
{
    unsigned int arr[8];
    int cnt,i,temp=0;
    FILE * input;
    if(!(input=fopen("C:/input.txt","r")))
    {
        fprintf(stderr,"cannot open file\n");
        exit(0);
    }
    cnt = fread(arr,1,8,input);
    for(i=0;i<cnt;i++)
    {
        printf("%d\n",arr[i]);
    }
    return 0;
}

any idea why?

3
  • Loop over the string with an accumulator to store the translated number for each place. Commented Jul 4, 2012 at 10:08
  • I've tried to loop it with accumlator but the problem is the every value of the array is very big, for example- 825241648 instead of 1 or 0. Commented Jul 4, 2012 at 10:12
  • Compare the sizes of the data items you are reading to the size of the variables your are storing the data read into, then review your code. Commented Jul 4, 2012 at 10:14

3 Answers 3

3

arr is an array of integers. But you read only 8 bytes into it. So your first integer will have some large value, and so will your second, but after that they will have garbage values. (You made arr an "automatic" variable, which is allocated on the stack, so it will have random garbage in it; if you made it a static variable it would be pre-initialized to zero bytes.)

If you change the declaration of arr so that it is of type char, you can read your string in, and your for loop will loop over those bytes one at a time.

Then you can write a string-to-binary translator, or alternatively you could use strtol() to do the conversion with the base set to 2. strtol() is not available in all compilers; GCC is fine but Microsoft C doesn't have it.

Sign up to request clarification or add additional context in comments.

1 Comment

strtol has been part of the C standard since early ANSI C drafts. Microsoft has not yet managed to write a strictly-conforming C compiler in over 20 years, so that might be why they lack that function.
2

Pl. see if the code (Compiled using gcc on Linux) below works for this.

#include<stdio.h>
#include<stdlib.h>
int main()
{
 char  arr[8];
 int cnt,i,temp=0;
 FILE * input;

if((input=fopen("data","r"))==NULL)
{
        fprintf(stderr,"cannot open file\n");
        exit(1);
}
 //Read the 8 bytes in a character array of size 8
 cnt = fread(arr,1,8,input);
 for (i = 0; i < cnt; i++)
 {
        //Now change it to 0/1 form by substracting 48
        arr[i] = arr[i] - '0';/* Ascii of 0 = 48*/
        //Also Left shift..
        arr[i] = arr[i] << (cnt - (i+1));
        //Now Bit wise OR with the integer...
        temp = temp | arr[i];
 }
  printf("The ascii value is %d and the character is %c\n", temp, temp);
  return 0;

}

1 Comment

There is a bug here. This code is shifting a char variable and then doing a logical-OR with temp. This means if the binary number is longer than 8 bits, this code won't work correctly. It would be much better to simply left-shift temp by 1 bit and then logical-OR in a new bit.
1

You first declare unsigned int arr[8]; which means 8 integers or more precisely 8*4=32 bytes. After that you read 8 bytes and then again try to output 8 integers. I suppose you want to read 8 bytes and output them as numbers? If you change type int to char, code might work. If file size is 32 bytes and contains integers, you may want to change fread() like this: fread(arr,sizeof(int),8,input);

1 Comment

you are right, if I change it to char, it read the value of 0 and 1 as it is. but is there a way to convert the array value from 01100001 to 97? without calculating every byte?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.