Thread: Conversion from BMP to binary file.. (Please help)

  1. #1
    Registered User
    Join Date
    Oct 2005
    Posts
    5

    Conversion from BMP to binary file.. (Please help)

    Hi All,

    I am new member for this forum.

    I want to convert to a BMP (24-bit) file in to binary format , then want to edit binary values in and again convert back to BMP file.

    Is it possible, through c or c++ or any tool is available for this.

    I am in a big need of this .

    Please help. me...

    With Reards,
    Pankaj Tyagi

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    BMP files are in a binary format.
    What's the question?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Hi Salem

    yes they are in binary format, but i want to edit BMP file in binary values (like in 10111011...)

    if i open this file in notpad then it is not showing me in binary values 10111011... .

    i need these values and after editing it , i want to get back modified BMP picture file (image).

    let me know if you need any other explanation.

    Thanks for response...

    With Regards,
    Pankaj Tyagi

  4. #4
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    This reminds me of Babbage's quote:
    Quote Originally Posted by Charles Babbage
    On two occasions I have been asked [by members of Parliament], ‘Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?’ I am not able rightly to apprehend the kind of confusion of ideas that could provoke such a question.
    Why on earth would you want to do that? (convert a bmp to the ASCII represenation of its binary and back again)

  5. #5
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    May be it can be funny to you. but see ....

    this is required in one Applied Optics project (regarding Holographic data storage) executed by some of IIT students.

    They have to edit photograph using some of their algos.. but to edit binary representation on this bmp file required, because editing is very complex and can be done only in binary form.

    if you guys have any idea regading this situtation, then please help me out.

    Thanks,
    Pankaj Tyagi

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It is to laugh.
    Code:
    while( (c = fgetc( yourfilepointer )) != EOF )
    {
        for( x = 0; x < CHAR_BIT; x++ )
            fputc( '0' + !!(c&(1<<x)), youtnewfilepointer );
    }
    I'll let you figure out how to convert it back. Oh, and ... nevermind.


    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Hardware Engineer
    Join Date
    Sep 2001
    Posts
    1,398

    Lightbulb A couple of suggestions...

    Go to wotsit.org and download the BMP format spec.

    Remember that everything in the computer's memory is already stored in binary. That means that once you've "decoded" the bitmap... once you have color-numbers for each pixel, you can perform binary-bitwise operations without any "conversion". (In fact, the most common text encryption technique is bitwise-exclusive-or, which is performed on ASCII values without any conversion.)

    By default C++ hides the binary from you, by automatically converting number values to/from decimal, and ASCII values that represent characters to/from alpha-numeric characters. It's actually a bit tricky to directly use binary I/O with C++.

    If you really need to "see" the binary representation, the normal way is to use hexadecimal. cin and cout can be used to input/output hex values directly. And, hex is much easier than binary (for humans) to read, especially if you're dealing with more than 16-bits. You can learn to convert between binary and hex in your head... Anybody studying Applied Optics can learn this in a couple of hours or less! Programmers (and hardware guys like me) almost always use hex, when working with binary numbers.

    There is a standard C++ header called <bitset> which makes working with and displaying binary numbers easier.

    EDIT -
    Get a Hex Editor. I think some versions of Microsoft Visual Studio come with one, or you can download one from the Net. A hex editor will allow you to open a bitmap file (or any other file). It will show you the hex values of every byte, an it will allow you to maually edit the bytes.

    Every hex editer I've seen will also display the ASCII character of every value that falls in the ASCII character range. (The hex editor doesn't "know" if a particular value is supposed to represent an ASCII character.)

    I'm pretty sure there is a Windows API function (or other microsoft library function) for copying the bitmap pixels into an array. That way, you wouldn't have to write the code to decode the bitmap format. You might search MSDN or post a question on the Windows forum.
    Last edited by DougDbug; 10-10-2005 at 01:10 PM.

  8. #8
    Dump Truck Internet valis's Avatar
    Join Date
    Jul 2005
    Posts
    357
    hex workshop and bless are both great

  9. #9
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Quote Originally Posted by pantya
    May be it can be funny to you. but see ....

    this is required in one Applied Optics project (regarding Holographic data storage) executed by some of IIT students.

    They have to edit photograph using some of their algos.. but to edit binary representation on this bmp file required, because editing is very complex and can be done only in binary form.

    if you guys have any idea regading this situtation, then please help me out.

    Thanks,
    Pankaj Tyagi
    You're confused. Perhaps what you mean is that you want to take a bmp file, strip out all the headers, un-compress (RLE) it if it's compressed, and convert it to a raw image bitpattern? If that's the case, the task makes sense.

    From what you appear to be originally asking, it seems you want to take a bmp file, which might have the first five bytes with values 83, 214, 10, 29, 62. And convert it to binary strings: "0101001111010110000010100001110100111110". But, this raises one small question:
    Why?

    The file is already in binary, you already have the raw values. The only thing doing the above achieves is (a) making the file 8 times bigger (b) making it more effort to manipulate.
    Last edited by cwr; 10-10-2005 at 07:36 PM. Reason: My "why" was too small.

  10. #10
    Registered User
    Join Date
    Oct 2005
    Posts
    5
    Guys, i really appreciate your help... Sorry for replying late

    I can understand all this...as i am also a programmer,

    but i have to contact to this research team (IIT students) and get back to you with reasons of why to do editng in binary values (because it was told to me specifically) and also suggest solutions given by all of you.

    Get back to you soon.

    With Thanks & Regards,
    Pankaj Tyagi

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Can we have vector of vector?
    By ketu1 in forum C++ Programming
    Replies: 24
    Last Post: 01-03-2008, 05:02 AM
  3. Post...
    By maxorator in forum C++ Programming
    Replies: 12
    Last Post: 10-11-2005, 08:39 AM
  4. Possible circular definition with singleton objects
    By techrolla in forum C++ Programming
    Replies: 3
    Last Post: 12-26-2004, 10:46 AM
  5. Header File Question(s)
    By AQWst in forum C++ Programming
    Replies: 10
    Last Post: 12-23-2004, 11:31 PM