Thread: How to read binary img file

  1. #1
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132

    How to read binary img file

    When reading a binary .img why is it when you read it by slurping in the file, then save it to a string and cout, you don't see the binary but random garbage?

  2. #2
    Registered User
    Join Date
    May 2010
    Posts
    4,632
    Probably because you're reading random garbage.

    How are you trying to read the file? Show your code.

    Jim

  3. #3
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    O_o

    How do you know you aren't seeing the binary?

    A text file is binary, but a binary file need not be text.

    Soma
    “Salem Was Wrong!” -- Pedant Necromancer
    “Four isn't random!” -- Gibbering Mouther

  4. #4
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    Quote Originally Posted by jimblumberg View Post
    Probably because you're reading random garbage.

    How are you trying to read the file? Show your code.

    Jim
    Code:
        ifstream ifs;
        ifs.open(argv[1], ios::binary);
        if(!ifs)
        {
            cerr << "failed to open file" << endl;
            return -1;
        }
        string content( (std::istreambuf_iterator<char>(ifs) ),
                           (std::istreambuf_iterator<char>()    ) );
        cout << content << endl;

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    What did you expect to see as output? Keep in mind post #3.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #6
    Registered User
    Join Date
    Apr 2010
    Location
    Vancouver
    Posts
    132
    Quote Originally Posted by laserlight View Post
    What did you expect to see as output? Keep in mind post #3.
    Wouldn't binary be 1s and 0s where as I see ���s�h1̼8�������e�E|3���l

  7. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by c_weed
    Wouldn't binary be 1s and 0s
    Look at what you wrote:
    Code:
    string content( (std::istreambuf_iterator<char>(ifs) ),
                       (std::istreambuf_iterator<char>()    ) );
    cout << content << endl;
    You used std::istreambuf_iterator to read bytes from the input stream into a std::string, then you printed the std::string. You're operating on the level of bytes, not bits. You are getting 1s and 0s, but they are in groups, presumably of 8, and interpreted in a group as a character.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by c_weed View Post
    Wouldn't binary be 1s and 0s where as I see ���s�h1̼8�������e�E|3���l
    Processors work on a byte level. You will never see raw 1s and 0s anywhere unless you explicitly pick out the bits from the bytes.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by c_weed View Post
    Wouldn't binary be 1s and 0s where as I see ���s�h1̼8�������e�E|3���l

    It will depend on what you are looking at it with, I would imagine. If you look at it with as text, you will see whatever the bits corresponding characters are in ANSI or UNICODE or whatever. If you look at it with a hex editor, you most likely see hexadecimal, which you could use to pick out specific things if you know the format of the data. It is all still the same data on the binary level.
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

  10. #10
    Registered User
    Join Date
    Nov 2013
    Posts
    107
    Quote Originally Posted by Alpo View Post
    It will depend on what you are looking at it with, I would imagine. If you look at it with as text, you will see whatever the bits corresponding characters are in ASCII or UNICODE or whatever. If you look at it with a hex editor, you most likely see hexadecimal, which you could use to pick out specific things if you know the format of the data. It is all still the same data on the binary level.
    You need to learn the fundamentals ASCII, UNICODE, character sets. Binary ASCII is encoded according to the ASCII character set from 0-255, ASCII - Wikipedia, the free encyclopedia

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    ASCII is an encoding, basically an interpretation of bits. It is defined through 0 - 127. The 7th bit is not defined in ASCII at all, but there are many "extended" ASCII sets in which the 7th bit is used to encode more symbols. These different "ASCII sets" are typically called locales and are the root of much evil™ (because the same bits mean different things in different locales!).
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Registered User Alpo's Avatar
    Join Date
    Apr 2014
    Posts
    877
    Quote Originally Posted by jim_0 View Post
    You need to learn the fundamentals ASCII, UNICODE, character sets. Binary ASCII is encoded according to the ASCII character set from 0-255, ASCII - Wikipedia, the free encyclopedia
    I don't understand what you mean? I know a text editor will interpret the value according to the code page it is using (with most mapping the original 7 bits to the normal ASCII characters, and the upper bit mapping to diacritics or whatever wants to be there). How does this modify that a text editor would interpret the values to be whatever characters they map to?
    WndProc = (2[b] || !(2[b])) ? SufferNobly : TakeArms;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Binary file read
    By BIGDENIRO in forum C Programming
    Replies: 4
    Last Post: 11-18-2013, 10:25 AM
  2. Replies: 12
    Last Post: 06-18-2012, 08:23 AM
  3. Binary read from file
    By dereach in forum C Programming
    Replies: 5
    Last Post: 02-27-2008, 03:34 PM
  4. Read binary file
    By Ken JS in forum C++ Programming
    Replies: 3
    Last Post: 05-29-2007, 11:12 AM