Convert NSString to NSUTF32StringEncoding [duplicate] - ios

This question already has answers here:
NSString to treat "regular english alphabets" and characters like emoji or japanese uniformly
(2 answers)
Closed 8 years ago.
I have an NSString that internally uses UTF16 encoding. I want to covert it to use UTF32 , so that
😄 or q both take single index. Currenty 😄 takes 2.
How to do this ?. Even if I can convert to some other type from NSString it will work. Bottom line is to have 😄 or q take equal number of indexes in an array.

Did you try :
NSData *data = [string dataUsingEncoding:NSUTF32StringEncoding];
NSString *convertedString = [[NSString alloc] initWithData:data encoding:NSUTF32StringEncoding];

Have you tried [[NSString alloc]initWithData:encoding:]?
Example would be:
NSData *data = [sourceStr dataUsingEncoding:NSUTF16StringEncoding];
Create your NSUTF32StringEncoded string from the data above
NSString *encodedStr = [[NSString alloc]initWithData:data encoding:NSUTF32StringEncoding];

Related

want to have this string "مرحبا 😀 Hello" save it to database and display back to uilabel

I am looking for solution where i want to store English + Arabic + Emoji Character to store to Database and retrieve it back while display.
Below is the code what i have used to support Emoji, after that Arabic text is not showing.
+(NSString *)emojiToSave:(NSString *)str
{
NSData *dataForEmoji = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *encodevalue = [[NSString alloc]initWithData:dataForEmoji encoding:NSUTF8StringEncoding];
return encodevalue;
}
+(NSString *)emojiToDisplay:(NSString *)str
{
NSData *msgData = [str dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodMsg = [[NSString alloc] initWithData:msgData encoding:NSNonLossyASCIIStringEncoding];
return goodMsg;
}
Can anyone pls suggest to give support for Arabic what change i should do?
Thanks in advance.
Try convert it into base64 code, then insert base64 code to database:
//Original string to base64 string
NSString *emojiString = #"مرحبا 😀 Hello";
NSData *emojiData = [emojiString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [emojiData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];
//Base64 string to original string
NSData *base64Data = [[NSData alloc] initWithBase64EncodedString:base64String options:NSDataBase64DecodingIgnoreUnknownCharacters];
NSString *originalString =[[NSString alloc] initWithData:base64Data encoding:NSUTF8StringEncoding];
NSLog(#"Result: %#",originalString);
Output:
You have to use an encoding that supports emoji and arabic characters. ASCII doesn't support that.
You should use NSUTF8StringEncoding everywhere, and you're fine.
Why are you using ASCII anyways? Why are you converting a string to an NSData and then back to NSString again? It doesn't make sense.

unable to show emoji's properly in UIlabel

I am trying to save the emojis to server and on later time receiving them.
I used the Following Code before
NSData *data = [strEmo dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *data1 = [strEmo dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data encoding:NSNonLossyASCIIStringEncoding];
It generates the Hashcode for the Emoji. But decoding doesn't work. So I skipped the Idea of using this Code.
Then I used a third Party NSString+HTML.h Class. Which Sends and recieve emoji easily. But
Now the Problem is when there are so many emoji's the UIlabel on which I am showing the data, the emojis are distorted and If I saved 20 emojis it shows 12-13 only.
I have added the Pic for reference
where Yellow part is UIlabel with back colored Yellow
Buddy why are you changing the string in Data two times in a row just simply use this
NSString *uniText = [NSString stringWithUTF8String:[strEmo UTF8String]];
NSData *msgData = [uniText dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *readyString = [[NSString alloc] initWithData:msgData encoding:NSUTF8StringEncoding];
First convert your string into constant C characters then convert it into string using UTF8 encoding, now convert it into NSData as (7-bit verbose ASCII to represent all Unicode characters) using NSNonLossyASCIIStringEncoding encoding and then again string ready to send with unicode characters. Hope this should work as it is working fine in my code.

How to display the emoji and special characters in UIlabel and UItextviews?

I am trying to display a string in all sorts of items such as UIlabel,UItextview,Uitextfield etc.....I am trying to do like this in a manner like this
NSData *data1 = [title dataUsingEncoding:NSUTF8StringEncoding];
NSString *goodValue = [[NSString alloc] initWithData:data1 encoding:NSNonLossyASCIIStringEncoding];
label.text=goodvalue;
this is working sometimes for me ,but some times it returns null for the string like this "Youtube\ud83d\ude27\ud83d\ude2e\ud83d\ude2f\ud83d".Can anybody guide me on this?
Emoji characters are in unicode plane 1 and thus require more than 16 bits to represent a code point. Thus two UTF8 representations or one UTF32 representation. Unicode is actually a 21-bit system and for plane 0 characters (basically everything except emoji) 16 bits is sufficient and we get by using 16 bits. Emoji need more than 16 bits.
"Youtube\ud83d\ude27\ud83d\ude2e\ud83d\ude2f\ud83d". is invalid, it is part of a utf16 unicode escaped string, the last \ud83d is 1/2 of an emoji character.
Also, inorder to create a literal string with the escape character "\" the escape character must be escaped: "\\".
NSString *emojiEscaped = #"Youtube\\ud83d\\ude27\\ud83d\\ude2e\\ud83d\\ude2f";
NSData *emojiData = [emojiEscaped dataUsingEncoding:NSUTF8StringEncoding];
NSString *emojiString = [[NSString alloc] initWithData:emojiData encoding:NSNonLossyASCIIStringEncoding];
NSLog(#"emojiString: %#", emojiString);
NSLog output:
emojiString: Youtube😧😮😯
The emoji string can also be expressed in utf32:
NSString *string = #"\U0001f627\U0001f62e\U0001f62f";
NSLog(#"string: %#", string);
NSLog output:
string1: 😧😮😯
NSString *str = #"Happy to help you \U0001F431";
NSData *data = [str dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *valueUnicode = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData *dataa = [valueUnicode dataUsingEncoding:NSUTF8StringEncoding];
NSString *valueEmoj = [[NSString alloc] initWithData:dataa encoding:NSNonLossyASCIIStringEncoding];
_lbl.text = valueEmoj;

NSString compare returning NSOrderedDescending instead of NSOrderedSame

I am receiving the data in an iPad application from a socket connected.
I am converting the data received to NSString using the method below:
NSString *data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding].
Then I am creating a substring from the string using the
NSString *substring1 = [data substringFromIndex:length-9]
NSString *substring2 = [data substringFromIndex:length-3]
where length is [data length].
Then I am comparing the substring2 with #"/>" string as below
[substring2 compare:#"/>"]
Here I checked the value of the substring2 while debugging the application the value is #"/>"
but the comparison result is returned as NSOrderedDescending instead of NSOrderedSame.
Can anyone please help?
Your string is having trailing space. The string which you are extracting as length - 3, it must be of length 3.
Now you are comparing it with #"/>" which is having length 2.
You need to do it be below way:
NSString *data = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding].
data = [data stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Now take the substring and compare.

Add double quote to NSString attach to the variable [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How to initialize NSString as text with double quotes
How can I add tan actual double quote to a variable inside on NSString to be used in NSDictionary?
Example
NSString *ename=#"John Doe";
NSDictionary *profile=#{#"Profile":#{"Name":ename}};
I need to display like this "Profile":{"Name":"John Doe"}
I need to display like this "Profile":{"Name":"John Doe"}
Do you want to create JSON format? Then you can use the built-in class NSJSONSerialization:
NSString *ename = #"John Doe";
NSDictionary *profile = #{#"Profile":#{#"Name":ename}};
NSData *jsonData = [NSJSONSerialization dataWithJSONObject:profile options:0 error:NULL];
NSString *jsonString = [[NSString alloc] initWithData:jsonData encoding:NSUTF8StringEncoding];
NSLog(#"%#", jsonString);
Output:
{"Profile":{"Name":"John Doe"}}
Use escape charater \ ...
NSString *ename=#"This is double \" quote \"";
If you are asking for adding quotes in Key of dictionary, then you have to add some special character, (non-alphabet and non-numerical) only then key shows double quotes.
[... description] simply wraps things in quotes for display that have non-alphanumeric characters in them.
And there is no difference between "abc" and abc in debugger. This is used only for dubugging and the actual value is always a NSString #"abc".
Check this
NSString *string=#"This is test \" for \"";
Hope it helps you..

Resources