First off, that's a non-portable call to reverse. You're assuming that an int is always 4 bytes. As was explained in this thread, that's not always the case. http://www.cplusplus.com/forum/general/105712/
A better call would have been:
reverse(sizeof(int),&data);
Assuming you're on a 32 bit platform, data contains 0x000005cd.
Reversing the order of the bytes results in 0xcd050000.
I am bit confuse on the implementation of reverse function.How is it reverse the code.I mean When I pass 1485. I was expecting 5841. But I know it is not the case with above code. it accept char pointer and access up to int
You declared 1485 as an int. An int is stored as binary.
You're expecting the decimal digits to be reversed. That would only happen if 1485 were stored as character data.
If you print tmp you will print it as if it was a C string, which is not the case. tmp might contain characters that can't be displayed properly and there might not be a null character to mark the end of the string.
hexadecimal has nothing to do with it. Data is stored as binary bits. Hex is simply a way of grouping those bits for easier representation.
The reverse function above works by reversing the order of some number of bytes (up to a limit of 24). How many bytes you reverse (and whether you need to at all), depends on the number representation on the source and target platforms.
Thanks a lot for sharing information.I admit my ignorance about number.
Have u seen any scenario where we need to reverse byte before write operation?.I could not find any purpose to reverse some byte before writing into file.
Lets keep the discussion of a single issue to a single thread. It gets confusing and redundant when you have multiple threads regarding the same issue.