Objective-C: How to create a method that will return a UInt8 array from an int

Posted by Adam on Stack Overflow See other posts from Stack Overflow or by Adam
Published on 2010-03-15T22:39:23Z Indexed on 2010/03/15 22:49 UTC
Read the original article Hit count: 434

Filed under:

I need to create an Objective-C method that converts an int into a byte array. In this instance, I can't use NSArray as a return type, it must be an UInt8 array. I've written a simple method to do this, but it has errors during compile and tells me that I have incompatible return types. Here is the code snippet. Any ideas?

 - (UInt8[])ConvertToBytes:(int) i 
{

     UInt8 *retVal[4];

     retVal[0] = i >> 24;
     retVal[1] = i >> 16;
     retVal[2] = i >> 8;
     retVal[3] = i >> 0;

     return retVal;
}

© Stack Overflow or respective owner

Related posts about objective-c