iOS Unicode Encoding - ios

I am getting this kind of data ElbowWristHand_DeQuervian\U00e2\U0080\U0099s Tenosynovitis through a web service, But actually the content is "ElbowWristHand_DeQuervian's". I have followed all the methods mentioned in the following link:
Using Objective C/Cocoa to unescape unicode characters, ie \u1234
but still i am unable to convert the unicode characters to the proper string. Please suggest.
Thanks

While initializing string us unicode string encoding -
NSString *dataString = [[NSString alloc] initWithData:recievedData usingEncoding:NSUnicodeStringEncoding]

Related

How to remove WhiteSpace between base64 string in objective c?

I referred to remove whitespaces from base64 encoded string when posting but still I have this problem
I am currently using the following code :
NSString *TrimBase64String=[base64String stringByReplacingOccurrencesOfString:#" " withString:#""];
Easy Peasy, use this line:
NSString *TrimBase64String=[base64String stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
Use below lines of code to create NSString with Base64 Encoding to avoid white spaces and then post it:
NSString *strbase64 = [YourNSData base64EncodedStringWithOptions:NSDataBase64EncodingEndLineWithLineFeed];
Check answer here, may be it will help more:
encode image to base64, get a invalid base64 string (ios using base64EncodedStringWithOptions)
I tried many ways and finally found the cause of the problem is the character \r
\r = CR (Carriage Return) // Used as a new line character in Mac OS before X`
[yourBase64String stringByReplacingOccurrencesOfString:#"\r" withString:#""];

Can't decode NSString well

I had my server encoding in ISO-8859-1 and decided to chango into UTF-8. The problem is that the filenames creates in ISO now crashes because I change the encoding type in my code. I try to connect with an iOS app and show the directories and files, the news are shown well but the olds with ISO not.
How can I detect if the filename is in one or other encoding to process in the right way each one? Because now, the filename in ISO it can be represent in UTF-8 but the string to access is not the same. (Ex: %E1p%F1 --> %C3%A1p%C3%B1)
I try this, but it doesn't work:
NSString *isoL = [item.href stringByAddingPercentEscapesUsingEncoding:NSISOLatin1StringEncoding];
char const *iso2utf = [isoL UTF8String];
NSString *utf = [[NSString alloc]initWithCString:iso2utf encoding:NSUTF8StringEncoding];
You need to have the server declare the content encoding of the response, much like how HTTP does.
I don't believe it's possible to auto-detect ISO-8859-1/UTF-8 text encoding.

how to remove characters '\U0000fffc' from nsstring ios

I am developing chat application. In iOS 8, i am sending text and image, while sending image with text a characters "\U0000fffc" are prefixed to nsstring.
I tried using the below code,but it is not working
NSCharacterSet *characterset=[NSCharacterSet characterSetWithCharactersInString:#"\\U0000fffc"];
NSString *newString = [str stringByTrimmingCharactersInSet:characterset];
Any help would be appreciated
Thanks to All who have answered and contributed. I fixed using below code
NSString *codeString = #"\uFFFC";
NSString *msg=[msg stringByReplacingOccurrencesOfString:codeString withString:#""];
If I used above code then compiled errors are fixed and worked....!
NSString *str = #"This is my string \U0000fffc";
NSString *strModified = [str stringByReplacingOccurrencesOfString:#"\U0000fffc" withString:#""];
NSLog(#"%#",strModified);
Try this. Hope will work for you. :)
Today, I have faced the same situation and after digging in some more I have found that the said character is in fact NSAttachmentCharacter which the function -(void)replaceCharactersInRange:(NSRange)range withAttributedString:(NSAttributedString *)attrString; adds to your attributed string to mark the position of a NSTextAttachment in your string and you shouldn't replace it anyways.
If you are still facing trouble working with your Strings, provide more information about the problem, otherwise, stick to your current implementation.
This is the replacement character � (often a black diamond with a white question mark or an empty square box) is a symbol found in the Unicode standard at codepoint U+FFFD in the Specials table. It is used to indicate problems when a system is unable to render a stream of data to a correct symbol. It is usually seen when the data is invalid and does not match any character.
This can be removed as follow
NSString *codeString = #"\\ufffc";
NSString *newString=[str stringByReplacingOccurrencesOfString:codeString withString:#""];
P.S : "/uFFFC" didn't worked for me.

iOS loading Base 64 encoded data in webview

I am trying to load base64 encoded data in a webview.Following is the code i am using
NSDictionary *fileFormatDic=[[NSDictionary alloc]initWithObjectsAndKeys:#"text/html",#"html",#"mage/jpeg",#"jpe",#"image/jpeg",#"jpeg",#"image/jpeg",#"jpg",#"application/pdf",#"pdf",#"application/vnd.ms-powerpoint",#"pdf",#"text/plain",#"txt",#"application/vnd.ms-excel",#"xls",#"application/msword",#"doc",nil];
NSData *fileData= [dbServiceMgr getDocumentDataForTheTaskId:[_fileDetailsDic objectForKey:#"taskId"] andFileId:[_fileDetailsDic objectForKey:#"fileId"]];
NSLog(#"filedata length---%d",fileData.length);
NSString *mimeType=[fileFormatDic objectForKey:[_fileDetailsDic objectForKey:#"fileFormat"]];
[_webView loadData:fileData MIMEType:mimeType textEncodingName:#"Base64" baseURL:nil];
But nothing is displayed in my webview. Can someone tell me, what is going wrong. Thanks in advance
I'm not sure that this is the reason for which you get a white screen (could be) but you must understand that textEncodingName expects a text encoding (meaning a method to encode a Unicode string to byte sequences) and you are providing a binary data-to-text encoding. So it's a different kind of encoding so to speak. Valid values would be UTF8 or UTF16 (althoulgh UTF8 is what you usually want). So, you should first decode your Base64 data and then present it to the screen.
You can read more about Unicode encodings here.

iOS special characters, strange issue

I've got a very strange issue. I started an iOS App about three years ago (iOS-SDK 3.0), and since then went through the SDKs 4.0 and 5.0. Since 5.0 (or maybe 5.1) I suddenly started having problems with German special chars (ä ö ü ß).
Now I can't even initialize an NSString with special chars, this line:
NSString *str = #"abcäxyz";
gives the following warning:
Input conversion stopped due to an input byte that does not belong to the input codeset UTF-8
And this one:
NSLog(#"%#", strTemp);
gives:
abc
So it's stopping at the first special char. In other projects everything is fine. I can work with special chars without any problems.
Is it a configuration problem?
EDIT: Obviously it is a problem with the file encoding.
file -I myFile
is giving:
text/x-c++; charset=unknown-8bit
Trying to convert it with iconv gives me:
conversion from unknown-8bit unsupported
What happens when you use the UTF-8 codes to initialize the string? Like so:
NSString *s = [NSString stringWithFormat:#"%C", 0xc39f]; // should be ß
As far as I know you should also be able to do this, but haven't tested it:
NSString *s = [NSString stringWithUTF8String:"0xc39f"];
Try those and see what happens. There's a number of sites around that keep UTF-8 code tables for special characters, e.g. this one.
As long as your file is encoded UTF-8, #"abcäxyz" should be fine, but the explicit form of embedding a literal unicode characters is \u????.
- (void)testGermanChar
{
NSString *expected = #"abc\u00E4xyz";
NSString *actual = #"abcäxyz";
STAssertEqualObjects(expected, actual, #"the two strings should be equivalent");
}
SOLVED: Changed the file encoding in Xcode:
Click on the file you want to change the encoding of, then open the right panel (whats the name of this pane actually? any idea?) to edit the properties. There you see "Text Encoding" under "Text Settings". That is all.

Resources