Instance Method

getCString:maxLength:encoding:

Converts the string to a given encoding and stores it in a buffer.

Declaration

- (BOOL)getCString:(char *)buffer maxLength:(NSUInteger)maxBufferCount encoding:(NSStringEncoding)encoding;

Parameters

buffer

Upon return, contains the converted C-string plus the NULL termination byte. The buffer must include room for maxBufferCount bytes.

maxBufferCount

The maximum number of bytes in the string to return in buffer (including the NULL termination byte).

encoding

The encoding for the returned C string. For possible values, see NSStringEncoding.

Return Value

YES if the operation was successful, otherwise NO. Returns NO if conversion is not possible due to encoding errors or if buffer is too small.

Discussion

Note that in the treatment of the maxBufferCount argument, this method differs from the deprecated getCString:maxLength: method which it replaces. (The buffer should include room for maxBufferCount bytes; this number should accommodate the expected size of the return value plus the NULL termination byte, which this method adds.)

You can use canBeConvertedToEncoding: to check whether a string can be losslessly converted to encoding. If it can’t, you can use dataUsingEncoding:allowLossyConversion: to get a C-string representation using encoding, allowing some loss of information (note that the data returned by dataUsingEncoding:allowLossyConversion: is not a strict C-string since it does not have a NULL terminator).

See Also

Getting C Strings

- cStringUsingEncoding:

Returns a representation of the string as a C string using a given encoding.

UTF8String

A null-terminated UTF8 representation of the string.

Beta Software

This documentation contains preliminary information about an API or technology in development. This information is subject to change, and software implemented according to this documentation should be tested with final operating system software.

Learn more about using Apple's beta software