convert NSString to UTF-16 compatible format - iphone

I am working on emojis with iPhone. I want to convert something like "\u1F604" (NSString) to "\U0001F604"(NSString) format.
How can i do that?
Thanks,
Jim.

Please try the below code.
NSString* resultTemp = #"\u1F604";
NSData* data = [resultTemp dataUsingEncoding:NSUTF16StringEncoding];
NSString* result = [[NSString alloc] initWithData:data encoding:NSUTF16StringEncoding];
self.detailDescriptionLabel.text = result;

Related

iPhone: strange output when casting nsdata to nsstring

I get a NSData object in return after decrypting a payload with aes128:
NSData *returnData = [ciphertext AES128DecryptWithKey:keyData withIV:ivData];
I get the following hex output when i try to NSLog this:
<2db88b73 d84599a1 5779c736 09c975b7 92750cf2 d11cb41b 19f13781
4401bc57 b2ad96c8 402e3ccf 851c0219 00aec76b>
I then try to setting it as NSString:
[[NSString alloc] initWithData:returnData
encoding:NSUTF8StringEncoding];
When using NSLog() on the string i get "(null)" as output.
Can someone tell me why and where i should look for the problem?
Collided with the same issue some time ago, found the answer here.
If the data is not null-terminated, you should use -initWithData:encoding:
NSString* newStr = [[[NSString alloc] initWithData:theData
encoding:NSUTF8StringEncoding] autorelease];
If the data is null-terminated, you should instead use -stringWithUTF8String: to avoid the extra \0 at the end.
NSString* newStr = [NSString stringWithUTF8String:[theData bytes]];
(If you have ARC enabled, remove the -autorelease call.)

convert emojis into unicode dynamically

How should i convert the emojis into unicode and send it to server and then can decode it dynamically.
I can statically give the if condition for perticular emojis.But i want to make it dynamically.
Thanks in advance
few days ago i have same problem. i tried this its work for me :
to pass value to the server
NSData *data = [textView.text dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding] autorelease];
goodValue = [goodValue stringByReplacingOccurrencesOfString:#"\\" withString:#"\\\\"];
when fetch value from the server
NSString *goodValue = [StaticClass urlDecode:[dataDict objectForKey:#"comment_text"]];
NSData *newdata=[goodValue dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
NSString *mystring=[[NSString alloc] initWithData:newdata encoding:NSNonLossyASCIIStringEncoding];

Xcode IPhone Parse Server Output

I am sending some data to the server that is coming back as a string, but i need to parse it at the carriage return (new line)
The variable the output is being stored in is
NSString *serverOutput = [[NSString alloc] initWithData: dataurl encoding:NSASCIIStringEncoding];
The output is:
1bac92c2-b83a-4c93-9e11-831115825fa9\nmike1\nmike2\nfoo#bar.ca\n2
Any suggestions or help?
Something like this:
NSString *serverOutput = #"1bac92c2-b83a-4c93-9e11-831115825fa9\nmike1\nmike2\nfoo#bar.ca\n2";
NSArray *myData = [serverOutput componentsSeparatedByString:#"\n"];

iPhone - Convert NSString encoding from WindowsCP1251 to UTF8

how can I have this conversion from NSWindowsCP1251StringEncoding to UTF-8?
I had several attempts but no one worked as it should. My last try was:
NSData *dt = [mystr dataUsingEncoding:NSUTF8StringEncoding];
NSString *str = [NSString alloc] initWithData:dt encoding:NSWindowsCP1251StringEncoding];
The result of str is unreadable. Did anyone encounter anything similar?
I think you were so close:
// Convert it back to CP1251
NSData *dt = [mystr dataUsingEncoding:NSWindowsCP1251StringEncoding];
// Now load it as UTF8
NSString *str = [NSString alloc] initWithData:dt encoding:NSUTF8StringEncoding];

how to decode UTF8 string in iphone

I want to decode an UT8 encoded string.
The input string is "øæ-test-2.txt"
and after decoding it should become
"øæ-test-2.txt"
I found many API to encode the NSString or NSData to UT8 (NSUTF8StringEncoding) but was not able to find the way to decode it.
What I have tried until now:-
NSString *str = [[NSString alloc] initWithUTF8String:[strToDecode cStringUsingEncoding:NSUTF8StringEncoding]];
AND
[strToDecode stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding]
AND
[NSString stringWithUTF8String:[strToDecode cStringUsingEncoding:[NSString defaultCStringEncoding]]]
I have tried the same input string and I get the proper output in third party decoder.
But was not able to get success
Any hint in right direction would be highly appreciated.
I use this one.
NSString *encoded_string = #"ü";
const char *ch = [encoded_string cStringUsingEncoding:NSISOLatin1StringEncoding];
NSString *decode_string = [[NSString alloc]initWithCString:ch encoding:NSUTF8StringEncoding];
NSLog(#"%#",decode_String)
[NSString stringWithUTF8String:]