Thread: reordering bytes in array

  1. #1
    Registered User
    Join Date
    Jan 2014
    Posts
    62

    reordering bytes in array

    Don't know why I am struggling hard with this but I have an array of bytes like this:
    Code:
    {
            0x28,0x83,0x63,0x20,0x79,
            0xE8,0x39,0x1A,0x59,0x04,
            0x89,0x10,0x8D,0x2E,0xF1,
            0x11,0x6E,0x00,0x10,0x8D,
            0x51,0x57,0x29,0x0D
        }
    start pointer points to 0x79 and end pointer points to 0x1A. I want start to look like this:
    Code:
    {0x1A, 0x39, 0xE8, 0x79}
    . In other words I want the bytes in reverse and have start point to that reverse order.


    Code:
    void reverse_order(unsigned char *start, unsigned char *end)
    {
        printf("What does start refer to 0x%x", *start);
        printf("What does end refer to 0x%x", *end);
        int i = 4;
        do {
            *start = *end;
            ++start;
            --end;
            printf("What is value of start 0x%x\n", *start);
            --i;
        } while(i > 0);
    }
    The output of above looks like this:

    Code:
    What does start refer to 0x79What does end refer to 0x1aWhat is value of start 0xe8
    What is value of start 0x39
    What is value of start 0x1a
    What is value of start 0x59
    Not sure why this isn't working.

  2. #2
    Registered User
    Join Date
    May 2009
    Posts
    4,181
    Try using i = 2.

    You need to do 2 swaps not 4.

    Tim S.
    "...a computer is a stupid machine with the ability to do incredibly smart things, while computer programmers are smart people with the ability to do incredibly stupid things. They are,in short, a perfect match.." Bill Bryson

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting bytes into float array
    By samtal in forum C++ Programming
    Replies: 4
    Last Post: 11-27-2011, 03:45 AM
  2. Reordering strings!
    By Monochrome in forum C Programming
    Replies: 4
    Last Post: 11-02-2011, 09:19 AM
  3. Problem with array of bytes
    By cioannou in forum C++ Programming
    Replies: 1
    Last Post: 02-22-2010, 06:01 AM
  4. concatenating 2 bytes of an array
    By davo666 in forum C Programming
    Replies: 1
    Last Post: 02-28-2009, 07:37 AM
  5. Reordering numbers
    By shaheel in forum C Programming
    Replies: 3
    Last Post: 11-22-2007, 08:56 AM