const char*
is assumed to point at a null-terminated string, so the first null character in the NSData
object will terminate the string.
Performing this conversion using a cast does not make sense out of context, since it's just asking the API to interpret an NSData as a const char*
.
If you are trying to pass an NSData to an API that wants const char*
, you are likely to be using the wrong API for your purpose, or you need to re-encode your data before using the API.
Update: Based on the OP's comment, he wants to encode the data to decode it later.
There are a variety of different solutions for this, but one simple possibility is to base64
encode the data using the API method. You could then base64
decode it using the symmmetric API method. You could then convert the resulting NSString
to const char*
using the approach suggested in this answer.