Thread: Endian swapping (byte-order reversal) on a file

  1. #1
    Registered User
    Join Date
    May 2007

    Endian swapping (byte-order reversal) on a file

    OK, so I found the C programming posts by a few people about byte-order swapping, and I've got that working just fine, but I'm a little confused as to how to grab those informations from a file, convert it, and then save that to a different file.

    #include <cstdio>
    #include <cstdlib>
    #include <climits>
    #include <iostream>
    #include <stdint.h>
    using namespace std;
    main(int argc, char *argv[])
        FILE* originalSave;
        FILE* newSave;
        long fSize;
        uint32_t *buffer;
        int x;
        uint32_t input = 0, j, result = 0, temp, final = 0, k;
        originalSave = fopen(argv[1], "rb");
        if (originalSave == NULL) {fputs("File error",stderr); exit(1);}
        newSave = fopen("newSave.fla", "wb+");
        fseek(originalSave, 0, SEEK_END);
        fSize = ftell(originalSave);
        buffer = (char*) malloc(sizeof(char)*fSize);
        if (buffer == NULL)
            fputs("Memory error",stderr); exit(2);
        fread(buffer, 1, 4, originalSave);
        result = ((buffer << 24) & 0xFF000000) | 
            ((buffer << 8) & 0x00FF0000) |
            ((buffer >> 8) & 0x0000FF00) |
            ((buffer >> 24) & 0X000000FF);
        printf("Original hex: %X\n;Swapped hex: %X\n", buffer, result);
        //fwrite(buffer, fSize/4, 4, newSave);
        cout << "Press ENTER to continue...";
        cin.ignore( std::numeric_limits<std::streamsize>::max(), '\n' );
      return 0;
    I don't even know if my code is functional (I get an error about invalid char or int to binary "operator>>" or whatever about the swap lines), but the structure should work, right? I'm completely confused by this.

    Oh, and eventually, I want to be able to do this with a file of up to 128KB size, so...

  2. #2
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    New Zealand
    You're trying to endian-swap the pointer itself, instead of the integer it points to!
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  3. #3
    Registered User
    Join Date
    May 2007
    OK, so I put in the pointer reference (&), and I still get those errors... Here's what they are exactly:

    main.cpp invalid operands of types `uint32_t**' and `int' to binary `operator>>'
    That's for all four lines with the bit shifts in them.

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    So & is soooo not "the pointer reference". If you want what the pointer points to, you need to dereference the pointer, using *.

  5. #5
    Registered User
    Join Date
    May 2007
    OK, so I dereference it. That works. It makes it compile, and so I run it (drag and drop) with a file that has hex values of 043E7A4F. But my output confuses me... I get:
    Original hex: 3E24E0
    ;Swapped hex: 43E7A4F
    First of all, how do I make it register the leading 0's in the values and not drop them, and second, is my file not really 043E7A4F if my program reads it as 3E24E0? Or am I having it read it wrong?

    EDIT: OK, that's odd... I change two parts of the program that shouldn't have anything to do with the output, and I sort of get it. (P.S. I changed the fwrite portion at the end to have it be "fwrite(buffer, 1, 4..." instead, and I took the "+" off of the "wb" in the newSave fopen). So now I've got:
    Original hex: 4F7A3E04
    ;Swapped hex:43E7A4F
    But that's backwards... The original hex is actually the swapped one, and vice versa...

    Oh, and I've been leaving the last fwrite/fclose/fclose/free section uncommented, but it's also still not creating the new file...

    EDIT 2: Using fwrite on the original save stream doesn't even work...
    Last edited by Molokai; 10-16-2009 at 08:23 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. gcc link external library
    By spank in forum C Programming
    Replies: 6
    Last Post: 08-08-2007, 03:44 PM
  3. About aes
    By gumit in forum C Programming
    Replies: 13
    Last Post: 10-24-2006, 03:42 PM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  5. Replies: 3
    Last Post: 03-04-2005, 02:46 PM