Thread: integer representation problem

  1. #1
    Registered User mikahell's Avatar
    Join Date
    Jun 2006

    integer representation problem

    I have a problem with the way integers are saved to memory. I want to read in binary from a file that contains some ints and other kinds of variables. I read every byte one by one, not four by four. I use this way to try to do a kind of file copy app by myself. But then it seems that the data is reversed.

    Lets say I want to store integer number 1 on a file, I hope it is in binary:
    00000000 00000000 00000000 00000001
    but then when I read it one byte at a time, I'd get this:
    00000001 00000000 00000000 00000000
    So it seem to me that things are reversed, and I found out the same while trying to do the same directly on some stacked memory... But is there any reason for this? Can it just be my compiler? Anyway, this is what is making the thing (well that's a sentence..):

    int a = 1;
    UCHAR *b;
    b = (UCHAR*)&a;
    std::cout	<< (int)(*(UCHAR*)b)
    		<< (int)(*(UCHAR*)(b + 1))
    		<< (int)(*(UCHAR*)(b + 2))
    		<< (int)(*(UCHAR*)(b + 3));
    //Displays 1000... Not 0001

  2. #2
    Registered User
    Join Date
    Aug 2001
    you may want too see what endianess/byte order means:

    e.g. the bytes of an integerscan be stored in memory EITHER with the most significant byte first - or with the least significant byte first.
    signature under construction

  3. #3
    Registered User mikahell's Avatar
    Join Date
    Jun 2006
    Ok then we have to deal with this... But I think it should be easier if it was Big-Endian then, as it eases our thinking to the way we normally write numbers...

  4. #4
    Registered User
    Join Date
    May 2006
    Berkshire, UK
    I hate to point out the obvious (actually I rather like it) but the way that the numbers are stored is more about helping the coputer to operate more efficiently than about you being able to play around with the parts of a number. Normally you would be insulated from this by using a high level programming language... c++ for example. Oh, you are and you are giving up one of the most useful features!

    Why do you actually need to do this? Perhaps there is another approach?

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    If you are writing a kind of file-copy application then endianess is probably irrelevant. You read the bytes in and write them out in the same order, whatever order that might be.
    You should also be careful to read in and write out a decent number of bytes at a time, that is a power of two in size e.g. 32768 bytes. That way it will be much faster.

  6. #6
    Registered User
    Join Date
    Oct 2001
    How was the file created? You could reconstruct the int like this (assumes an int is 32 bits and a byte is 8 bits):
       int a;
       UCHAR *b;
       //Read from file into buffer b
       a = *b | *(b+1)<<8 | *(b+2)<<16 | *(b+3)<<24;
       cout << hex << a << endl;

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    The edge of the known universe
    I think you're over analysing the problem.

    If you want a portable solution, use a text file and do
    fout << myInt; // save
    fin >> myInt; // load

    If you don't care so much, and just want to save the binary representation, then do
    fout.write( &myInt, sizeof myInt ); &myInt, sizeof myInt );
    Yes, this may have an endian problem if you try and move the file to a different machine architecture. It may also have a size problem and an alignment problem as well.

    But if it's just for the local machine and the data is private to that program, then it should work fine.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Small Problem with double and integer adding?
    By Nathan the noob in forum C++ Programming
    Replies: 5
    Last Post: 03-28-2009, 04:16 PM
  2. memory issue
    By t014y in forum C Programming
    Replies: 2
    Last Post: 02-21-2009, 12:37 AM
  3. Bin packing problem....
    By 81N4RY_DR460N in forum C++ Programming
    Replies: 0
    Last Post: 08-01-2005, 05:20 AM
  4. Problem with cin. Please help me.
    By Antigloss in forum C++ Programming
    Replies: 17
    Last Post: 06-06-2005, 09:50 AM
  5. A homework problem about C++ (Pointer)
    By joenching in forum C++ Programming
    Replies: 10
    Last Post: 03-14-2005, 04:28 PM