Created
February 14, 2014 10:18
-
-
Save phnessu4/8998813 to your computer and use it in GitHub Desktop.
字节序转换
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <CFByteOrder.h> | |
// Bytes to uint32_t | |
NSData *data = <THE_DATA>; | |
void *bytes = [data bytes]; | |
uint32_t *intBytes = (NSInteger*)bytes; | |
uint32_t swapped = CFSwapInt32BigToHost(*intBytes); ///< If the data in `data' is big endian | |
// uint32_t to bytes | |
uint32_t someInt = 1234; | |
uint32_t swappedInt = CFSwapInt32HostToBig(someInt); ///< If we want to store in big endian | |
NSData *data = [NSData dataWithBytes:&swappedInt length:sizeof(swappedInt)]; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment