Thread: Storing values, internal mechanisms?

  1. #1
    Registered User
    Join Date
    Oct 2007
    Posts
    22

    Storing values, internal mechanisms?

    Hi all

    I got a silly question...I actually came to this "problem" when looking at endianess.
    I have the following test-code:

    Code:
    #include <stdio.h>
    #include <string.h>
    #include <stdlib.h>
    
    int main()
    {
        FILE *out = fopen("test.txt", "w");
        char test[] = "this is a test!\n";
    
        int *p = malloc(sizeof(int));
        memcpy(p, test, sizeof(int));
    
        printf("&#37;x\n", *p);
    
        fwrite(p, 1, sizeof(int), out);
    
        fclose(out);
    
    
        return 0;
    }
    So. The value of "p" gets printed in hex:
    0x73696874
    This would match: siht, so "this" spelled backwards. Its a little endian machine, so small-end stored first, therefore it's 73 ('s') that should be the biggest value and is printed first when interpreted as an integer.
    But the following rest remains a riddle...

    The code above also writes a file "test.txt" and stores the value of "p". Hexdump reveals:
    0000000 6874 7369 -->matches "htsi"
    0000004
    But when reading the file "byte by byte" with "fgetc(...)" and output it hex I get:
    74 68 69 73
    which would match "this" again.

    So there are 3 different forms now. Why ? I don't get it...
    Anybody an idea?
    Last edited by plan7; 09-07-2008 at 06:39 AM.

  2. #2
    Registered User
    Join Date
    Oct 2007
    Posts
    22
    Ok, got the answer myself, could hit me myself now...

    As I pointed out, this:
    0x73696874
    is because it's a little endian machine. "this" interpreted as an integer.
    Hexdump reveals:
    Quote:
    0000000 6874 7369 -->matches "htsi"
    0000004
    This is actually as my hexdump takes two bytes as default and displays these as HEX. So it's again because of little endian --> first two bytes ("th") in reverse order ("ht" or 0x68 0x74) and same for the second two bytes. How great...

    But when reading the file "byte by byte" with "fgetc(...)" and output it hex I get:
    Quote:
    74 68 69 73
    which would match "this" again.
    Well, the whole thing is stored in the file as it's in memory, same order. Little endian just interprets these 4 bytes differently as big endian machine, but saves it the same way.

    Riddle solved, can sleep again tonight.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Storing values in strings
    By scotty4598 in forum C Programming
    Replies: 9
    Last Post: 04-29-2009, 10:15 PM
  2. Storing values into a dynamic array
    By porsche911nfs in forum C++ Programming
    Replies: 5
    Last Post: 04-24-2009, 09:08 AM
  3. reading values from a file
    By megastar in forum C Programming
    Replies: 4
    Last Post: 06-25-2007, 02:08 AM
  4. storing values.....NEED HELP.
    By new2tehgame in forum C++ Programming
    Replies: 9
    Last Post: 09-14-2005, 08:17 PM
  5. Struct Values
    By Muphin in forum C++ Programming
    Replies: 5
    Last Post: 08-13-2005, 09:24 PM