Function

CFStringCreateExternalRepresentation

Creates an “external representation” of a CFString object, that is, a CFData object.

Declaration

CFDataRef CFStringCreateExternalRepresentation(CFAllocatorRef alloc, CFStringRef theString, CFStringEncoding encoding, UInt8 lossByte);

Parameters

alloc

The allocator to use to allocate memory for the new CFData object. Pass NULL or kCFAllocatorDefault to use the current default allocator.

theString

The string to convert to an external representation.

encoding

The string encoding to use for the external representation.

lossByte

The character value to assign to characters that cannot be converted to the requested encoding. Pass 0 if you want conversion to stop at the first such error; if this happens, the function returns NULL.

Return Value

A CFData object that stores the characters of the CFString object as an “external representation.” Returns NULL if no loss byte was specified and the function could not convert the characters to the specified encoding. Ownership follows the The Create Rule.

Discussion

In the CFData object form, the string can be written to disk as a file or be sent out over a network. If the encoding of the characters in the data object is Unicode, the function may insert a BOM (byte-order marker) to indicate endianness. However, representations created with encoding constants kCFStringEncodingUTF16BE, kCFStringEncodingUTF16LE, kCFStringEncodingUTF32BE, and kCFStringEncodingUTF32LE do not include a BOM because the byte order is explicitly indicated by the letters “BE” (big-endian) and “LE” (little-endian).

This function allows the specification of a “loss byte” to represent characters that cannot be converted to the requested encoding.

When you create an external representation from a CFMutableString object, it loses this mutability characteristic when it is converted back to a CFString object.

The CFStringCreateFromExternalRepresentation function complements this function by creating a CFString object from an “external representation” CFData object.

See Also

Accessing Characters

CFStringGetBytes

Fetches a range of the characters from a string into a byte buffer after converting the characters to a specified encoding.

CFStringGetCharacterAtIndex

Returns the Unicode character at a specified location in a string.

CFStringGetCharacters

Copies a range of the Unicode characters from a string to a user-provided buffer.

CFStringGetCharactersPtr

Quickly obtains a pointer to the contents of a string as a buffer of Unicode characters.

CFStringGetCharacterFromInlineBuffer

Returns the Unicode character at a specific location in an in-line buffer.

CFStringGetCString

Copies the character contents of a string to a local C string buffer after converting the characters to a given encoding.

CFStringGetCStringPtr

Quickly obtains a pointer to a C-string buffer containing the characters of a string in a given encoding.

CFStringGetLength

Returns the number (in terms of UTF-16 code pairs) of Unicode characters in a string.

CFStringGetPascalString

Copies the character contents of a CFString object to a local Pascal string buffer after converting the characters to a requested encoding.

CFStringGetPascalStringPtr

Quickly obtains a pointer to a Pascal buffer containing the characters of a string in a given encoding.

CFStringGetRangeOfComposedCharactersAtIndex

Returns the range of the composed character sequence at a specified index.

CFStringInitInlineBuffer

Initializes an in-line buffer to use for efficient access of a CFString object's characters.

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