String Services gives you a number of tools for converting between string encodings. Some routines do the actual conversions while others show which encodings are available and help you chose the best encoding for the current situation.
If you want to convert between any two non-Unicode encodings, you can use a CFString object as an intermediary. Say you have a string encoded as Windows Latin 1 and you want to encode it as Mac OS Roman. Just convert the string to Unicode first (the CFString object), then convert the CFString's contents to the desired encoding.
Many of the creation and content-accessing functions described in earlier sections of this document include an encoding parameter typed
CFStringEncoding
. These functions are listed in Table 1. To specify the encoding of the source or destination string (depending on whether you're creating a CFString object or accessing its contents), specify the
enum
value for the desired encoding in this parameter when you call one of these functions. Use the
CFStringIsEncodingAvailable
function to test for the availability of an "external" encoding on your system before you call a conversion function .
Converts from CFString (Unicode) |
---|
CFStringGetPascalString
|
CFStringGetCString
|
CFStringGetPascalStringPtr
|
CFStringGetCStringPtr
|
CFStringGetBytes
|
CFStringCreateExternalRepresentation
|
A word of caution: not all conversions are guaranteed to be successful. This is particularly true if you are trying to convert a CFString object with characters that map to a variety of character sets. For example, let's say you have a Unicode string that includes ASCII characters and accented Latin characters. You could convert this string to Mac OS Roman but not to Mac OS Japanese. In these cases, you can specify "lossy" conversion using the
CFStringGetBytes
function; this kind of conversion substitutes a "loss" character for each character that cannot be converted. The
CFStringGetBytes
function is described in the next section