How to convert a CGPDFStringRef to unicode char? I have used CGPDFStringCopyTextString to get the string and then [string characterAtIndex:i] to cast to unichar, is this the right way? or is there any way to get the bytes of the string and convert to unicode directly? Need some guidance here.
Convert or Print CGPDFStringRef string
2.3k views Asked by Lunayo At
3
There are 3 answers
2
On
It's not a bad idea, even if you can access the CGPDFString directly using CGPDFStringGetBytePtr
. You will also need CGPDFStringGetLength
to get the string length, as it may not be null-terminated.
See the documentation for more info
0
On
although UPT's answer is correct, it will produce a memory leak
from the documentation: CGPDFStringCopyTextString "...You are responsible for releasing this object."
the correct way to do this would be:
CFStringRef _res = CGPDFStringCopyTextString(pdfString);
NSString *result = [NSString stringWithString:(__bridge NSString *)_res];
CFRelease(_res);
NSString is capable of handling of unicode characters itself, you just need to convert the CGPDFString to NSString and further you can use it as follows: