I want to convert binary file to ascii. how to do that in C

I have tried some methods of reading single values into a bool array, but haven't got it working. I have however done a test with reading in single chars and then bitwise AND-ing them to discover which bits are set. Here's a simple example of how I did it.

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
    unsigned char ch;

/*  '0' = 0x30 = %00110000
    '1' = 0x31 = %00110001   */

    ofstream ostream("test.txt", ios::binary);
    ostream.write("1", 1);
    ostream.close();

    ifstream istream("test.txt");
    ch = istream.get();

    printf("%d", (ch&0x80)/0x80);
    printf("%d", (ch&0x40)/0x40);
    printf("%d", (ch&0x20)/0x20);
    printf("%d", (ch&0x10)/0x10);
    printf("%d", (ch&0x08)/0x08);
    printf("%d", (ch&0x04)/0x04);
    printf("%d", (ch&0x02)/0x02);
    printf("%d\n", (ch&0x01)/0x01);
   //Output: 00110001

    istream.close();

    cin.ignore();
    return 0;
}

I notice that you specified "how to do that in C". If you mean that only FILE pointers should be used, then instead of istream.get(), you could use fgetc();

Also, the numerous printf() - statements are just to clarify. You better just loop through as such:

for(unsigned i=0x80; i>0; i/=2){
        printf("%d", (ch&i)/i);
    }
    putchar('\n');

I want to convert binary file to ascii. how to do that in C

Depends completely on what you need. Be less vague.

And if you want to now in C, why not post in the C forum, not C++ forum.

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.