Convert nsstring with UTF8 encoding in it? - encoding

I have an nsstring #"I\xe2\x80\x99m" that I want to convert to the NSString #"I'm" any idea how to do that? Thanks!

The NSString literal #"I\xe2\x80\x99m" already is "I’m", there is no conversion necessary.
Demonstration:
#include <Foundation/Foundation.h>
int main()
{
NSLog(#"I\xe2\x80\x99m");
return 0;
}
Program output:
2013-05-21 14:12:33.471 a.out[452:707] I’m
The escape sequences you put in your source code are interpreted in UTF-8, and the resulting UTF-8 is made into an NSString object.

Related

Convert or Print CGPDFStringRef string

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

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 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…).