Thread: Reading file, endianess, int

  1. #1
    Registered User
    Join Date
    Feb 2012
    Posts
    2

    Reading file, endianess, int

    Hello, everyone

    I've wanted to start learning more about C++ after been in C# for about 2years.

    So I wanted to do this new project into C++ instead, yet I'm struggling so much with such simple idiotic thing, but I can't wrap my head around.

    I have a raw file..
    Code:
    41000000000000002A000000F4790100....
    The very thing I want is to get the first 4 bytes and display as a int.
    That means, show me "65"

    I've tried countless things, but can't in a simple way
    and the closest I got was using a for loop... definetly not on the simple side...

    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    int main()
    {
        char b[4];
        std::ifstream f("file",ios::binary);
        f.seekg(0);
        f.read(b,4);
        cout << hex << b;
        return 0;
    }
    This displays me "A" thats kinda right, but not what I want
    I was expecting atleast 41, or 41000000 or 00000041

    I've tried to cast it as (unsigned int)b, but then it display some random hex numbers..

    What am I doing it wrong, is there a simple way to do this at all?

    In c# i'd just use.
    int i = b.ReadInt32();
    //b being a binaryreader stream of the file.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Enzo Maganha
    The very thing I want is to get the first 4 bytes and display as a int.
    That means, show me "65"
    How are you interpreting those 4 bytes as an int?
    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

  3. #3
    Registered User
    Join Date
    Feb 2012
    Posts
    2
    Quote Originally Posted by laserlight View Post
    How are you interpreting those 4 bytes as an int?
    Gotta to thank ya, since your question did lead me to an answer..

    that was using
    Code:
    int x;
    f.read((char*)&x,4);
    but I think I cheated, as I don't even understand fully what I am doing here..

    reading 4 bytes of the file pointed by char into the reference to the int x?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,412
    Quote Originally Posted by Enzo Maganha
    reading 4 bytes of the file pointed by char into the reference to the int x?
    Basically, you are interpreting those first four bytes as the bytes of an int as it would have been written - endianness aside - by the write member function of an ostream.
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 07-17-2011, 03:51 AM
  2. Endianess of machine
    By fsanchez in forum C Programming
    Replies: 9
    Last Post: 08-25-2010, 10:37 AM
  3. endianess problem!!!!
    By sandeepvignesh in forum Networking/Device Communication
    Replies: 2
    Last Post: 06-17-2009, 04:58 AM
  4. Bitshift with different endianess
    By TriKri in forum C Programming
    Replies: 5
    Last Post: 06-25-2008, 12:37 PM
  5. enumeration/endianess
    By Nyda in forum C Programming
    Replies: 3
    Last Post: 10-12-2004, 04:54 PM