objective c - How would I convert this data from Little Endian to Big Endian -


i'm working on solution outputting data in little endian, need convert in objective-c, what's best way approach this.

conceptually understand what's going on, i'm struggling code make happen.

the data being outputted in cbcharacteristic.

update:

- (void)peripheral:(cbperipheral *)peripheral didupdatevalueforcharacteristic:(cbcharacteristic *)characteristic error:(nserror *)error {     if (error) {         nslog(@"error changing notification state: %@", [error localizeddescription]);     } else {          // extract data characteristic's value property         // , display value based on characteristic type         nsdata *databytes = characteristic.value;         if ([characteristic.uuid isequal:[cbuuid uuidwithstring:lock_occupancy_char]]) {             nslog(@"%@",databytes);         }     } } 

this output log: log: <79660000 7a660000 27671b80 0700>

starting left right values represented as: start time, end time, mac address.

the timestamps in unix epoch.

based on example above seem reading data device -- , data being packaged in single nsdata buffer. since it's known format - known size can read structure can access each field, , process if needed.

char rawdata[] = {0x79, 0x66, 0x00, 0x00, 0x7a, 0x66, 0x00, 0x00, 0x27, 0x67, 0x1b, 0x80,0x07, 0x00}; nsdata *sampledata = [nsdata datawithbytes:rawdata length:sizeof(rawdata)];  typedef struct mystruct {     uint32_t starttime;     uint32_t endtime;     uint8_t macaddress[6]; } mystruct;  // first step - break buffer structure  mystruct sample; [sampledata getbytes:&sample length:sizeof(sample)];  // second step - if needed need process each member of structure  // cfswapint32bigtohost documented take known big endian (network order) format // current cpu supports.   i'm not 100% sure sample data has reasonable timestamps // can't verify if sample data needs swapped or not  sample.starttime = cfswapint32bigtohost(sample.starttime); sample.endtime = cfswapint32bigtohost(sample.endtime); 

the act of changing little endian big endian called 'byte swapping' - need know byte order of current cpu uses (in theory can change based on hardware target) - ios, both simulator (x86) , devices (arm) both same (little endian).

since mention using apple framework, i'd recommend using apple's corefoundation functions work.

https://developer.apple.com/library/ios/documentation/corefoundation/conceptual/cfmemorymgmt/tasks/byteswapping.html


Comments