Thread: Pack & unpack values

  1. #1
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459

    Pack & unpack values

    G'day,

    I'm currently writing some endian-indepedant code. Specifically from files in little-endian. I have a few functions in place to swap the bytes of a character array:

    Code:
    static void endian_swap_bytes(char * array, size_t n)
    {
        size_t i = 0;
        size_t half = 0;
        char tmp;
    
        if(array == NULL || n <= 1)
            return;
    
        half = n / 2;
        while(i < half)
        {
            --n;
    
            tmp = array[i];
            array[i] = array[n];
            array[n] = tmp;
    
            ++i;
        }
    
        return;
    }
    What's the most portable way to unpack/pack the values of the array into say, integer variables? Float variables and alike? I could use bit shifting etc, but is there a better way?

    Thanks in advance

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    x86 has a "bswap" instruction that swaps 32 bits from one endian to the other (that is, little to big or big to little - it's the same operation both ways). This operation is done much quicker than a loop over 4 bytes. So what I'm suggesting is that you should consider having a generic routine, but also special cases so that you can make use of such "fast" functionality when it's available. Ideally, this should be done in such a way that it's inline-able, as there will be a huge benefit of not even calling a function when translating simple 32-bit integers.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Hmm thanks. I was avoiding stdint.h because it wasn't a C89 header... and really I can't live with out it . And since the bytes are read from file I needed to garantee they were a certain length (ie, not an "int" which could be whatever).

    Cheers for your help, I'll bang up a few functions now.

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Not sure why you would need stdint.h - the purpose of my comment was more that you may need want to specialize individual size types so that they can be done quickly when the suitable options are available.

    Going back to your original question, for arrays, you probably need to write a function that loops through the array and converts all values in it. It gets even better when you get to structs, of course.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    Because my plan was to read the values into character arrays (and write from character arrays) to ensure an int was exactly 32 bits when written to the file and read as 32 bits. Regardless of the parent platform... hence the packing / unpacking issue at the end.

    Otherwise, I've now done what you've said . I've got special little functions, otherwise a big clunky one is used. Once I'm done I'll run it through a CPU emulator to see how it goes.

    Thanks for your help, really!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 1
    Last Post: 12-30-2007, 10:08 AM
  2. Need help with project
    By chrisa777 in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2006, 05:01 PM
  3. Replies: 1
    Last Post: 06-03-2005, 01:03 AM
  4. Replies: 1
    Last Post: 02-03-2005, 03:33 AM
  5. pack and unpack
    By soonerfan in forum C Programming
    Replies: 0
    Last Post: 12-03-2001, 07:17 AM