Reading file, endianess, int

This is a discussion on Reading file, endianess, int within the C++ Programming forums, part of the General Programming Boards category; Hello, everyone I've wanted to start learning more about C++ after been in C# for about 2years. So I wanted ...

  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
    20,955
    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?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    20,955
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21