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?
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?
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.
#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.
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)
Please use code tags when posting code in the future. You've gotten yourself all mixed up with dereferencing and incrementing:
You really need to review how to use pointers. Here are a couple tutorials: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
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;
Ohh Ya i really need to go thru pointers properly.
Thanks so much. You solved my problem
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.