Is there a way to get the length in bytes of a CFString given an arbitrary character encoding? It seems possible because the function CFStringGetSmallestEncoding must do some calculations already, but I don’t want to use the smallest encoding, I want to find out how big a buffer I might need to allocate if I want the bytes in UTF-8 encoding or maybe another variable-width character encoding.
Is it possible? Or do I need to allocate the maximum possible buffer size using CFStringGetMaximumSizeForEncoding?
Do you have Foundation available? If so, you could cast the CFStringRef to an NSString* and use lengthOfBytesUsingEncoding:.
Keep in mind though that CFStringGetMaximumSizeForEncoding is O(1), whereas lengthOfBytesUsingEncoding: is O(n) in the length of the string (you are essentially doing the conversion twice), so you may still be better off using CFStringGetMaximumSizeForEncoding to allocate a buffer.