title: How to detect endianess to keep a consistent byte order with iOS
body: <p>I am having an issue with iOS byte orders. We are sending messages over BLE, some of the values are stored as NSInteger objects</p>
  
  <p>I have an older (but still supported) iPad appears to store integers with big-endian byte order which is required for our peripheral to correctly interpret the value, however when I run the same application on a newer iPhone it doesn't work and the endianness is reversed.</p>
  
  <pre><code>NSInteger number = 230; /* this is actually declared elsewhere, this is just for the example */
  
  short value = (short)number;
  
  NSData* data = [NSData dataWithBytes:(void*)&amp;value length:sizeof(value)];
  
  int intData = [self intFromNSData:data];
  </code></pre>
  
  <p>Result values coming through are</p>
  
  <pre><code>On iPad: 230 (00E6)
  On iPhone: 58880 (E600)
  </code></pre>
  
  <p>As you can see the byte order is different on the two devices, I just want to know how I can detect and, if necessary, swap the bytes before I send them to the peripheral device. Or is there a different way to store these numbers that will be consistent across all devices?</p>