So here's the thing, im reading a binary file wich is an image. I think im reading this correctly, so i have the whole binary info i red stored in a buffer and i wanna display this in my screen like '0100010010011' is there a way for me to do this? Help would be much appreciated im on a tight schedule, thanks in advance.

Loop through the buffer byte by byte and send each byte to a function that outputs the binary value of the parameter.

Look up boolean operators & | << >> for this function.

Where i have doubts is on the output.. heres what i tried:

fseek(fh , 0 , SEEK_END);
tam = ftell(fh);
rewind(fh);

buff = (char*) malloc (sizeof(char)*tam);
fread (buff,tam,1,fh);

for (c=0;c<tam;c++)
{
     printf("%c ", buff[c]);
}

But all i get on the screen is trash... If i write it like this:

printf("%.2d ", (int)buff[c]);

I get the hex... but what i really wanted was to see the '01001011' relative to each byte... since the buffer is of type char, even tough im reading the file using "rb" i tought i could write char by char and see that info...

You should try - itoa() function.

/* new variables */
 int b;
 char  bin_str[9]; // to hold binary string 

fseek(fh , 0 , SEEK_END);
tam = ftell(fh);
rewind(fh);

buff = (char*) malloc (sizeof(char)*tam);
fread (buff,tam,1,fh);

for (c=0;c<tam;c++)
{   
       b=buff[c];
       // itoa(int vaule,char *str,int radix) - stdlib.h
       itoa(b,bin_str,2);
       printf("\n%s",bin_str);
  }

I cant use itoa i get undefined reference... and the buffer where i have the binary info stored, is already type char... i really dont understand why when i try to type whats there as char i get trash.. since im going to have to convert whats there to decimal it would really be handy if i could see whats there as binary. Thanks anyways XD

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.