iOS and OS X Network Programming Cookbook
上QQ阅读APP看书,第一时间看更新

Finding the byte order of your device

In the Introduction section of this chapter, one of the concepts that was briefly discussed was how devices store information in memory (byte order). After that discussion, you may be wondering what the byte order of your device is.

Tip

The byte order of a device depends on the Microprocessor architecture being used by the device. You can pretty easily go on to the Internet and search for "Mac OS X i386 byte order" and find out what the byte order is, but where is the fun in that? We are developers, so let's see if we can figure it out with code.

We can determine the byte order of our devices with a few lines of C code; however, like most of the code in this book, we will put the C code within an Objective-C wrapper to make it easy to port to your projects. The downloadable code for this chapter contains the Objective-C classes within an application to test your system.

Getting ready

This recipe is compatible with both iOS and OS X. No extra frameworks or libraries are required.

How to do it…

Let's get started by defining an ENUM in our header file:

  1. We create an ENUM that will be used to identify the byte order of the system as shown in the following code:
     typedef NS_ENUM(NSUInteger, EndianType) {
        ENDIAN_UNKNOWN,
        ENDIAN_LITTLE,
        ENDIAN_BIG
    };
  2. To determine the byte order of the device, we will use the byteOrder method as shown in the following code:
    -( EndianType)byteOrder {
        union {
            short sNum;
            char cNum[sizeof(short)];
        } un;
        un.sNum = 0x0102;
        if (sizeof(short) == 2) {
            if(un.cNum[0] == 1 && un.cNum[1] == 2)
                return ENDIAN_BIG;
            else if (un.cNum[0] == 2 && un.cNum[1] == 1)
                return ENDIAN_LITTLE;
            else
                return ENDIAN_UNKNOWN;
        } else
            return ENDIAN_UNKNOWN;
    }

Tip

Downloading the example code

You can download the example code files for all Packt Publishing books you have purchased from your account at http://www.packtpub.com. If you purchased this book elsewhere, you can visit http://www.packtpub.com/support and register to have the files e-mailed directly to you.

How it works…

In the ByteOrder header file, we defined an ENUM with three constants. The constants are as follows:

  • ENDIAN_UNKNOWN: We are unable to determine the byte order of the device
  • ENDIAN_LITTLE: This specifies that the most significant bytes are last (little-endian)
  • ENDIAN_BIG: This specifies that the most significant bytes are first (big-endian)

The byteOrder method determines the byte order of our device and returns an integer that can be translated using the constants defined in the header file. To determine the byte order of our device, we begin by creating a union of short int and char[]. We then store the value 0x0102 in the union. Finally, we look at the character array to determine the order in which the integer was stored in the character array. If the number one was stored first, it means that the device uses big-endian; if the number two was stored first, it means that the device uses little-endian.

The downloadable code contains projects for both the Mac OS X and iOS devices, so you can see how to use this class and also test the byte order of your devices.