There’s a special NSString initWithData method for grabbing bits and converting them into string. However, I haven’t found that in NSNumber class ref. Currently, I’m getting raw data (bytes) from a server in NSData format. I know how to do that in C, using memcpy and int pointers. But I am curious about what the convenience methods are for doing that straight from NSData. No conversion is needed. For example, I’m getting 00000010 byte, and I need to turn that into NSNumber of value 2, or NSInteger.
There’s a special NSString initWithData method for grabbing bits and converting them into string.
Share
NSDatais just a bucket for bytes and has no knowledge of the data contained therein.NSString‘sinitWithData:encoding:method is a reciprocal (it does the opposite) of this method:Therefore, to answer your question fully, it’s important to know how your numbers were originally coerced into an
NSDataobject. Once you know the encoding function, the search is for the reciprocal function.From what you’ve included in the question, there may be a number of different solutions. However, you’ll probably be able to use something along the following lines to convert into a usable numeric format using
getBytes:length:on your NSData object. For e.g.You can change the type of
decodedIntegerto whatever is appropriate for the bytes in yourNSDataobject.