Reading binary file(image)

Please support our C advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Jun 2009
Posts: 7
Reputation: Benkyou is an unknown quantity at this point 
Solved Threads: 0
Benkyou Benkyou is offline Offline
Newbie Poster

Reading binary file(image)

 
0
  #1
Jun 5th, 2009
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.
Reply With Quote Quick reply to this message  
Join Date: May 2006
Posts: 3,127
Reputation: WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of WaltP has much to be proud of 
Solved Threads: 283
Moderator
WaltP's Avatar
WaltP WaltP is offline Offline
Posting Sensei

Re: Reading binary file(image)

 
0
  #2
Jun 5th, 2009
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.
The 3 Laws of the Procrastination Society:
1) Never do today that which can be put off until tomorrow
2) Tomorrow never comes
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 7
Reputation: Benkyou is an unknown quantity at this point 
Solved Threads: 0
Benkyou Benkyou is offline Offline
Newbie Poster

Re: Reading binary file(image)

 
0
  #3
Jun 5th, 2009
Where i have doubts is on the output.. heres what i tried:

  1. fseek(fh , 0 , SEEK_END);
  2. tam = ftell(fh);
  3. rewind(fh);
  4.  
  5. buff = (char*) malloc (sizeof(char)*tam);
  6. fread (buff,tam,1,fh);
  7.  
  8. for (c=0;c<tam;c++)
  9. {
  10. printf("%c ", buff[c]);
  11. }

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

  1. 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...
Reply With Quote Quick reply to this message  
Join Date: Oct 2008
Posts: 2,721
Reputation: adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of adatapost has much to be proud of 
Solved Threads: 501
Moderator
adatapost's Avatar
adatapost adatapost is offline Offline
Posting Maven

Re: Reading binary file(image)

 
0
  #4
Jun 5th, 2009
You should try - itoa() function.

  1. /* new variables */
  2. int b;
  3. char bin_str[9]; // to hold binary string
  4.  
  5. fseek(fh , 0 , SEEK_END);
  6. tam = ftell(fh);
  7. rewind(fh);
  8.  
  9. buff = (char*) malloc (sizeof(char)*tam);
  10. fread (buff,tam,1,fh);
  11.  
  12. for (c=0;c<tam;c++)
  13. {
  14. b=buff[c];
  15. // itoa(int vaule,char *str,int radix) - stdlib.h
  16. itoa(b,bin_str,2);
  17. printf("\n%s",bin_str);
  18. }
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 7
Reputation: Benkyou is an unknown quantity at this point 
Solved Threads: 0
Benkyou Benkyou is offline Offline
Newbie Poster

Re: Reading binary file(image)

 
0
  #5
Jun 5th, 2009
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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:




Views: 496 | Replies: 4
Thread Tools Search this Thread



Tag cloud for C
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC