iOS, sqlite3 & unicode char - ios

For my app I want to be able to use unicode caracters like \n, and these ones :
http://www.easyapns.com/category/just-for-fun
The text is stored in a sqlite3 database, and when I read it, I get for example the text \ue415 instead of the smiley. I neither have a line break, but a \n .
I'm able to display the smileys and line breaks using this piece of code :
NSString* unicodeString = [myString stringByReplacingOccurrencesOfString:#"\\n" withString:#"\n"];
unicodeString = [unicodeString stringByReplacingOccurrencesOfString:#"\\ue022" withString:#"\ue022"];
// etc...
But I would like to find a generic way to do this, in order to be able to display all the unicode caracters.
I'm getting the text from my sqlite3 database this way :
NSString* title = [NSString stringWithUTF8String:(char *)sqlite3_column_text(statement, 3)];
I tried to replace \\ by \, but it is not possible to write the string #"\", because it escapes the " ... I tried to replace \\\\ by \\, but it doesn't work. I also tried to get the string from the database using other encoding, without success...
Any idea ?

I finally found my solution there (not the accepted answer, the one from Nikolai Ruhe) :
Using Objective C/Cocoa to unescape unicode characters, ie \u1234
NSString* convertedString = [myString mutableCopy];
CFStringRef transform = CFSTR("Any-Hex/Java");
CFStringTransform((__bridge CFMutableStringRef)convertedString, NULL, transform, YES);
It does not escape the \n though, but it can easilly be done with stringByReplacingOccurrencesOfString.

Related

Insert space or enter in sqlite in ios sdk

I want to insert space or enter in sqlite database.
Is their any way to insert the space or enter.
Can we do it by ascii character.
We can do it by using nsattributed string and but can we do it by nsstring?
Yes, simply use \t (space - tab) or \n (enter - new line) with NSString.
e.g. NSString *test = [NSString stringWithFormat:#"This is an example\nTo show you the great things\t:D"]; and you can save it into your db, when you show it in a UILabel or UITextView \n and \twill convert to new line or tab space.
Update: even you can use html tags for this, use (space) and <br> for new line. Or some other tags, <pre> to show preformated text.
I'm not sure whether sqlite deny escape characters for space and new line, in case you can replay them with html tags.
I had solved this issue by below code:-
- (BOOL)textView:(CustomTextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text
{
str_Notes=[str_Notes stringByAppendingString:text]; //Where str_Notes is NSstring
return YES;
}
And then i use string in my insert query. and i is working perfectly fine.

Convert Unicode Emoji to NSString (Not \ue415 format)

I am trying to convert an emoji to an NSString. I previously asked a question on how to do the opposite (convert an NSString to a unicode) at NSString to Emoji Unicode. I thought perhaps it would be best to ask this as a new question here.
How can I convert an NSString containing an emoji (πŸ˜ƒ) to an NSString containing a unicode in this format (U0001F603)?
This question is basically the reverse engineering of the solution from the previous page. The catch is the project does not use the \ue415 format, but rather the U0001F603 format.
Edited per comment:
2014-07-11 11:37:19.448 emoticon[******] unicode: πŸ˜‚
unicode = [NSString stringWithFormat:#"%#\\UFE0E", unicode];
2014-07-11 11:37:19.449 emoticon[******] unicode: πŸ˜‚\UFE0E
SECOND COMMENT RESPONSE
I'm not entirely sure if I follow what you mean by I didn't add the first line of code. I hope I haven't been unclear. To try and be more specific on what I would like, I logged your code in, and then logged what I wish to get:
NSString *first = #"πŸ˜ƒ";
NSString *second = #"πŸ˜ƒ\\UFE0E";
NSString *third = #"U0001F603\\UFE0E";
2014-07-11 12:00:45.815 emoticon[******] first: πŸ˜ƒ, second: πŸ˜ƒ\UFE0E, third: U0001F603\UFE0E
2014-07-11 12:00:45.816 emoticon[******] desiredString: U0001F603
My hope is to produce the desiredString by converting the emoji to the desired string.
THIRD COMMENT RESPONSE
What you need is using the escape character \U0000FE0E to the end of all Unicode characters to make it skip the emoji and display the proper Unicode character.
Here's the code:
#"πŸ˜ƒ" //This shows the colorful emoji icon.
#"πŸ˜ƒ\U0000FE0E" //This shows the good old Unicode character.
You can also add it to the character code:
#"U0001F603\U0000FE0E"
Here is a "pseudo-code" (JavaScript, you can run it in your browser's console) ..for opposite-direction.
String.fromCharCode(
((0x1F603 - 0x10000) >> 10) | 0xD800
,
((0x1F603 - 0x10000) % 0x400) | 0xDC00
)
=>>"πŸ˜ƒ"
Just reverse the bytewise operations, and zero-pad it.
If you are a programmer it should be more than easy for you.
..give a man a fish...
source: JavaScript Ninja - Easy Unicode Emoji Generator 😁🌠🐬

IOS Convert URL string to NSString?

I have a problem to convert an URL string, which I extract from XML file to NSString.
The URL string look like this, it looks like odd but it is URL format.
%3CTEXTFORMAT%20LEADING%3D%222%22%3E%3CP%20ALIGN%3D%22LEFT%22%3E%3CFONT%20FACE%3D%22Arial%22%20SIZE%3D%2212%22%20COLOR%3D%22%23000000%22%20LETTERSPACING%3D%220%22%20KERNING%3D%220%22%3E%u53F0%u5317%u7E2323141%u65B0%u5E97%u6C11%u6B0A%u8DEF130%u5DF714%u865F5%u6A13%3C/FONT%3E%3C/P%3E%3C/TEXTFORMAT%3E
However, when I use stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding method, it return nil.
After some experiment and research, seems this URL contain %u cause problem while converting URL and this %u looks like unicode, however, I try to remove all %u then stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding method return a proper string without any problem.
Does anyone know how can I convert this URLstring to NSString properly?
It is Unicode han characters in your urlString thats why it is not converting.
Replace %u to \u and you will get your String.
NSString *str=#"%3CTEXTFORMAT%20LEADING%3D%222%22%3E%3CP%20ALIGN%3D%22LEFT%22%3E%3CFONT%20FACE %3D%22Arial%22%20SIZE%3D%2212%22%20COLOR%3D%22%23000000%22%20LETTERSPACING%3D%220%22%20KERNING%3D%220%22%3E%u53F0%u5317%u7E2323141%u65B0%u5E97%u6C11%u6B0A%u8DEF130%u5DF714%u865F5%u6A13%3C/FONT%3E%3C/P%3E%3C/TEXTFORMAT%3E";
str=[str stringByReplacingOccurrencesOfString:#"%u" withString:#"\\u"];
NSString *convertedStr=[str stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"converted string is %# \n",convertedStr);
output :---------------
converted string is <TEXTFORMAT LEADING="2"><P ALIGN="LEFT"><FONT FACE="Arial" SIZE="12" COLOR="#000000" LETTERSPACING="0" KERNING="0">\u53F0\u5317\u7E2323141\u65B0\u5E97\u6C11\u6B0A\u8DEF130\u5DF714\u865F5\u6A13</FONT></P></TEXTFORMAT>
for more Info follow this url
This is chinese unicode char
here is some code that will prove it:
NSString *newStr=#"\u53F0\u5317\u7E2323141\u65B0\u5E97\u6C11\u6B0A\u8DEF130\u5DF714\u865F5\u6A13";
NSLog(#"chinese string is %#",[newStr stringByReplacingPercentEscapesUsingEncoding:NSUTF16StringEncoding]);
output:----------------------
ε°εŒ—ηΈ£23141ζ–°εΊ—ζ°‘ζ¬Šθ·―130ε··14θ™Ÿ5樓
go to google translate converting this string will give you someone's address.
as :-
Citizens Xindian, Taipei County 23141 Road 130, 5th Floor, No. 14, Lane

NSDictionary objectForKey: string issue

I have a NSDictionary created with data from a web api.
Here is the dictionary logged:
{
chapter = {
text = "\n \tAmo\U00cc\U0081s";
};
}
When loging [dict objectForKey:#"chapter"] looks like this:
{
text = "\n \tAmo\U00cc\U0081s";
}
And when logging [dict objectForKey:#"text"] I get
AmoÌs
which is not correct, it should be Amo\U00cc\U0081s / AmΓ³s
It seems to be an encoding problem, but I can't figure it out.
Any idea why this is happening?
Thanks
The NSLog is printing correctly!!!
You can not print Unicode text to log. You have new line, a tab and \U00cc and \U0081 which is converting to some un-readalbe texts.
This is not a bug. CF and Cocoa interpret %S and %C differently from how printf and its cousins interpret them. CF and Cocoa treat the character(s) as UTF-16, whereas printf (presumably) treats them as UTF-32.
The CF/Cocoa interpretation is more useful when working with Core Services, as some APIs (such as the File Manager) will hand you text as an array of UniChars, not a CFString; as long as you null-terminate that array, you can use it with %S to print the string.
Copied from here.

NSString componentsSeparatedByString "&" but ignore "& amp;"

Need to separate a string on & but not on &.
Is there a more elegant way to code this up rather than just replacing it first then separating it on the &? Like this...
query = [query stringByReplacingOccurrencesOfString:#"&" withString:#"~~~"];
NSArray * kvpairs = [query componentsSeparatedByString:#"&"];
NSMutableArray *mArr = [[NSMutableArray alloc] init];
for (NSString *kvp in kvpairs) {
[mArr addObject:[kvp stringByReplacingOccurrencesOfString:#"~~~" withString:#"&"]];
}
kvpairs = [NSArray arrayWithArray:mArr];
[mArr release];
You could use NSRegularExpression to enumerate through the string on a regular expression that matches & but not &, e.g.: #"&(?!amp;)". This will be more cumbersome than your current method but more exact, because it will work without modifying the original string and doesn't rely on a token value.
If you control the input to this method and can guarantee that ~~~ won't appear normally, there's nothing wrong with using ~~~. However if you don't control the input then you should attempt to parse the string without modification.
There isn't really anything wrong with that, as long as you are 100% sure that the string ~~~ won't occur in your query.
If you are not, my next step would be to implement a method to parse the string into an array. In this method, you could find each &, then check if it is followed by amp;. If it is, move on to the next one, if it is not, cut the string there and repeat.
HTML entities and references are really not a good thing to have hanging around in URLs. Unless there is a very good reason that HTML entities must be used, I would recommend using '%26' in the URL encode an ampersand.
That being said, you will run into problems if '&' is not the only HTML entity or reference, so unless you are absolutely sure '&' is the only one, you will need a more robust solution.

Resources