Thread: Big Endian to Little Endian

  1. #1
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Big Endian to Little Endian

    I am trying to convert 64 bit vale from Big Endian to Little Endian for a 32 bit processor. Can anybody help me with this?

  2. #2
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, we can help you. Where are you stuck? What don't you get? Post your code in code tags. If you don't know where to start, look up some tutorials, like this one: Cprogramming.com FAQ > Bit shifting and bitwise operations.

  3. #3
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    #define RAW32(x) (Pkt->body.log.log_item.data.raw[x] + (Pkt-> body.log.log_item.data.raw[(x)+1] << 8) + \
    (Pkt->body.log.log_item.data.raw[(x)+2] << 16) + (Pkt->body.log.log_item.data.raw[(x)+3] << 24))


    unsigned long first = RAW32(21);
    unsigned long last = RAW32(25);

    unsigned long *ptr, *ptr1;
    double final_pos_lat, *temp;
    ptr = &first; /*adrress of first 4 bytes*/
    ptr1 = &last; /*adress of last 4 bytes*/
    final_pos_lat = *ptr1; /*first 4 bytes of data*/
    final_pos_lat = final_pos_lat + 4; /* my problem is here i am trying to increment the address of final_pos_lat by 4*/
    final_pos_lat = *ptr; /*copy next 4 bytes of data*/
    Last edited by AngelSmile; 10-27-2011 at 01:21 PM.

  4. #4
    Registered User
    Join Date
    Oct 2011
    Posts
    4
    Now I modified my code to:
    unsigned long *final;
    double final_pos_lat ;
    final = &last; /*copy last 4 bytes of data*/
    (*final) += 4;
    final = &first; /*copy first 4 bytes of data*/
    final_pos_lat = *final;

    But final_pos_lat has only the value of first 4 bytes*/

    Basically I am trying to copy first and last 4 bytes of data into double (8 byte variable)

  5. #5
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Please use code tags when posting code in the future. You've gotten yourself all mixed up with dereferencing and incrementing:
    Code:
    final = &last;  // this sets final to point to the unsigned long variable 'last'
    (*final) += 4;  // this dereferences final (effectively giving you 'last') and adds 4 to it, i.e. it's like last += 4
    final = &first;  // set final to point to the unsigned long variable 'first'
    final_pos_lat = *final;  // dereference final, giving you 'first', and assign that value to final_pos_lat
    You really need to review how to use pointers. Here are a couple tutorials:
    Pointers in C - Tutorial - Cprogramming.com
    Eternally Confuzzled - All About Pointers

    Instead, you can use the array operator to assign the two halves:
    Code:
    unsigned ong *final;
    double final_pos_lat;
    final = (unsigned long *) &final_pos_lat;  // set final to the location we want to write to
    final[0] = first;  // you may want to switch whether [0] is first or last
    final[1] = last;

  6. #6
    Registered User
    Join Date
    Oct 2011
    Posts
    4

    Thumbs up

    Ohh Ya i really need to go thru pointers properly.
    Thanks so much. You solved my problem

  7. #7
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Are you doing this just as a study of bitwise operators or something? There are functions that can do this for you in any environment where this is going to actually matter. Look up htons and ntohs. There are a couple of other additional functions that go along with those, but that will get you started.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Little endian and Bit endian advantages and disadvantages
    By nkrao123@gmail. in forum C Programming
    Replies: 4
    Last Post: 09-11-2011, 04:40 AM
  2. Big endian
    By kiros88 in forum Linux Programming
    Replies: 6
    Last Post: 05-21-2010, 08:08 PM
  3. big endian-small endian problem
    By kapil1089thekin in forum C Programming
    Replies: 3
    Last Post: 05-15-2008, 06:47 PM
  4. Big Endian Little Endian Complex- Converting Characters
    By bd02eagle in forum C Programming
    Replies: 3
    Last Post: 07-11-2006, 01:01 AM
  5. Big endian/Little endian conversion
    By bonkey in forum Windows Programming
    Replies: 5
    Last Post: 09-25-2002, 02:29 PM