| | |
Reading binary file(image)
Please support our C advertiser: Programming Forums - DaniWeb Sister Site
![]() |
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
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.
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
Where i have doubts is on the output.. heres what i tried:
But all i get on the screen is trash... If i write it like this:
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...
C Syntax (Toggle Plain Text)
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:
C Syntax (Toggle Plain Text)
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.
C Syntax (Toggle Plain Text)
/* 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); }
•
•
Join Date: Jun 2009
Posts: 7
Reputation:
Solved Threads: 0
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
![]() |
Similar Threads
- Reading binary file without knowing file format (C)
- reading a binary file in C (C)
- Problems reading a Binary File more than 2.0 GB with VC++ (C++)
- Reading Binary Files in VB (Visual Basic 4 / 5 / 6)
- fscanf and reading from binary file (C)
- read binary file into 2-Dimensional Array (VB.NET)
- Reading a Binary File to a C++ Class (C++)
Other Threads in the C Forum
- Previous Thread: Passing linked list to function, segmentation fault
- Next Thread: Find CPU temperature with C code (in Linux)
Views: 496 | Replies: 4
| Thread Tools | Search this Thread |
Tag cloud for C
adobe ansi api array arrays bash binarysearch centimeter char convert copyanyfile copypdffile cprogramme createcopyoffile createprocess() csyntax directory drawing dynamic executable fflush file fork frequency getlasterror givemetehcodez global graphics gtkgcurlcompiling hardware highest homework i/o inches infiniteloop initialization interest km lazy linked linkedlist linux linuxsegmentationfault list locate logical_drives match matrix meter microsoft motherboard multi mysql open opendocumentformat opensource openwebfoundation owf pattern pdf performance pointer pointers posix power problem probleminc program programming pyramidusingturboccodes read recursion recv repetition scanf scheduling segmentationfault send shape socketprograming spoonfeeding stack standard strchr string strings structures student suggestions system systemcall test testautomation unix user voidmain() wab win32 win32api windows.h






