Convert or Print CGPDFStringRef string - iphone

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.

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:
NSString *tempStr = (NSString *)CGPDFStringCopyTextString(objectString);

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);

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

Related

How to convert an NSString which is NSUTF8Encoded to NSASCIIEncoding in objective-c iPhone?

I have an NSString which contains data encoded with NSUTF8Encoding. I want to convert that string into NSASCIIEncoding. Please tell me anyway to convert it in a proper manner. I am able to convert reverse (NSASCIIEncoding to NSUTF8Encoding).
Please provide any sample code.
Thanks in advance
I have an NSString which contains data encoded with NSUTF8Encoding.
Really? Because an NSString always treats its contents as UTF-16 internally. Because you cannot be sure how an NSString stores its data internally. Conceptually, NSString works with UTF-16.
I want to convert that string into NSASCIIStringEncoding.
if ([myString canBeConvertedToEncoding:NSASCIIStringEncoding]) {
const char *asciiString = [myString cStringUsingEncoding:NSASCIIStringEncoding];
}

Different kind of UTF8 decoding in NSString

I have searched a lot about UTF8 decoding, but not found the answer yet.
I receive an UTF-8 decode NSString from my NSXMLParser:
NSString *tempString = #"Test message readability is óké";
In someway I can't find the way to change this encoded text to:
Test message readability is óké
I could tell all the options I tried but I don't think that should be necessary. Could please some help?
Thnx!
The NSXMLParser will treat the text using the character encoding that the XML specifies. I believe in your case the XML do not specify UTF-8 explicitly.
The text seems to be ISO Latin 1. If you can not do anything about the server generating the XML then you can apply this hack:
char* tempString = [string cStringUsingEncoding:NSISOLatin1StringEncoding];
string = [NSString stringWithUTF8String:tempString];
I have verified that this works by testing this from the GDB prompt:
po [NSString stringWithUTF8String:(char*)[#"Test message readability is óké" cStringUsingEncoding:5]]
You're doing it wrong. What you want is:
char *s = "Test message readability is óké";
//Note: this is a one-byte-character C string, not an NSString!
NSString *tempString = [NSString stringWithCString:s encoding:NSUTF8StringEncoding];
Also keep in mind that when you initialize string constants, what actually goes to program memory depends on the encoding of the current file. If it's already UTF-8, then the characters will be doubly-encoded - you'll get characters Ã,³, etc. encoded as UTF8 in the C string.
In other words, using a string constant is probably a wrong move to begin with. Please give more context to the problem.
Standart encoding and decoding like this:
For encoding:
NSString *content = [bodyTextView.text stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
For decoding:
NSString *decodedString = [msg.content stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];

NSString returning jibberish

Totally lost with this one. Here's my code:
theColor = [NSString stringWithFormat:#"white"];
NSLog(#"%s", theColor);
Which is returing:
†t†å
I must be doing something stupid, but can not figure it out for the life of me.
Change your print to:
NSLog(#"%#", theColor);
Hope it helps.
The thing is that %s expects a C-string (char array with a NULL terminator) and you are passing a NSString instance which is not the same as a C-string. The modifier you need in a format to print NSString content is %#.
%s is for printing C-style strings.
%# is for printing Objective-C objects (like NSString).
BTW: “theColor = [NSString stringWithFormat:#"white"];” – why not “theColor = #"white";”?
Greetings

How to get the string value from a string which contains ","?

I have a string value where it contains comma. Ex:- 1,234. I want to get the value of the string where i need only 1234. Can you please help me...
Instead of manually stripping out the commas, it might be more elegant (and less error-prone if you support different locales) to use an NSNumberFormatter to convert the string to a number.
NSString *myString = "1,234";
NSString *resultString = [myString stringByReplacingOccurrencesOfString:#"," withString:#""];
If you want to strip the comma then: -
NSString *string = #"1,234";
string = [string stringByReplacingOccurrencesOfString:#"," withString:#""];
This should return you a string with just 1234 in it.
By 'getting the value' do you mean, converting this to a NSNumber object? If so use this
NSNumber *numberFromString = [NSNumber numberWithInteger:[string integerValue]];
I don't know that framework/language, but if an integer converter won't work, then strip out the commas by replacing them with null from the string and then convert to an integer.

How can I decode a utf8 NSstring on iphone?

I have an NSstring that is encoded with UTF8. I want to decode it.
I tried this but it doesn't work:
NSString *decodedString = [NSString stringWithUTF8String:[encodedString cStringUsingEncoding:[NSString defaultCStringEncoding]]];
How can I do that?
Thanks
Sending cStringUsingEncoding: to encodedString can only work if encodedString is an NSString object. Having an NSString object is the point you're trying to get to. Moreover, that method does not decode the string, it encodes the string using the encoding you ask for; thus, in the code you show, you ask for the (already-encoded) string to be encoded in some random encoding (the default C string encoding), then attempt to decode the result as UTF-8. That won't work.
You don't specify what type you're using for encodedString. Either way, you need to create the NSString object by passing encodedString and its encoding.
If encodedString is an NSData object, use the initWithData:encoding: method.
If it's a null-terminated C string; use the initWithCString:encoding: method or (only when it's UTF-8) the initWithUTF8String: method.
If it's just plain (unterminated) bytes, use the initWithBytes:length:encoding: method.
All of these except initWithUTF8String: require you to specify the encoding (initWithUTF8String: does by definition), and all of them except initWithData:encoding: come in an autoreleasing convenience version (+stringWith… instead of -initWith…).