How to encode some text correctly - iphone

I have some text formatting issues that I need to solve. I have some strange characters displaying from the NSString below
the original string:
NSString *descriptionStringPreFormatted = [item objectForKey:#"title"];
the formatted string:
NSString *descriptionLabelStringUTF8 = [descriptionStringPreFormatted stringByReplacingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSLog(#"descriptionStringPreFormatted is %#", descriptionStringPreFormatted);
NSLog(#"descriptionLabelStringUTF8 is %#", descriptionLabelStringUTF8);
here's the output which is the same whether I use the UTF8 encoding or not.
the output:
2013-01-05 16:44:51.807 descriptionStringPreFormatted is £144.99...
2013-01-05 16:44:51.810 descriptionLabelStringUTF8 is £144.99...

I think you are receiving dictionary "item" from web services. So try to decode that response string from webservice with NSUTF8StringEncoding.
NSString *str=[[NSString alloc] initWithData:responseData encoding:NSUTF8StringEncoding];
here "responseData" is raw data coming from web services.

Related

Conversion of NSData to NSString fails due to some charecters

I am converting NSData to NSString which I got as response of a url using the following method.
NSString *result = [[NSString alloc] initWithData:_Data encoding:NSUTF8StringEncoding];
It works fine and I am using this for a long time but today I faced an issue while loading the data (paging) at one page my result gives null string.
So I searched SO and found a method from this link NSData to NSString converstion problem!
[NSString stringWithCString:[theData bytes] length:[theData length]];
and this works fine.
My queries,
The method was deprecated in iOS 2.0. If I use this will I be facing any issue in future?
I think this is the text that made the method fail What is this and is there any way that I can encode this using NSUTF8StringEncoding?
What is the the alternative encoding that I can use for encoding all the type of characters like in the above pic?
In order to obtain the type of the content which is sent by the server, you need to inspect the Content-Type header of the response.
The content type's value specifies a "MIMI type", e.g.:
Content-Type: text/plain
A Content-Type's value may additionally specify a character encoding, e.g.:
Content-Type: text/plain; charset=utf-8
Each MIME type should define a "default" charset, which is to be used when there is no charset parameter specified.
For text/* media types the default charset is US-ASCII.
(see RFC 6657, §3).
The following code snippet demonstrates how to safely encode the body of a response:
- (NSString*) bodyString {
CFStringEncoding cfEncoding = NSASCIIStringEncoding;
NSString* textEncodingName = self.response.textEncodingName;
if (textEncodingName) {
cfEncoding = CFStringConvertIANACharSetNameToEncoding( (__bridge CFStringRef)(textEncodingName) );
}
if (cfEncoding != kCFStringEncodingInvalidId) {
NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(cfEncoding);
return [[NSString alloc] initWithData:self.body encoding:encoding];
}
else {
return [self.body description];
}
}
Note:
body is a property returning a NSData object representing the response data.
response is a property returning the NSHTTPURLResponse object.
If
NSString *result = [[NSString alloc] initWithData:_Data encoding:NSUTF8StringEncoding];
returns nil then _Data does not contain a valid string in UTF-8 encoding.
You said that
[NSString stringWithCString:[theData bytes] length:[theData length]];
works fine in your case. This method
interprets the data bytes in the "default C string encoding", but it is unspecified which
encoding that is (and therefore this method is deprecated and should not be used).
I think the default C string encoding is still "Mac Roman". In that case
NSString *result = [[NSString alloc] initWithData:_Data encoding:NSMacOSRomanStringEncoding];
would be the correct solution. But in any case, you should find out which encoding
the web service uses for the response, and specify that in the initWithData:encoding:
method.
Try this
NSString *theString = [NSString stringWithFormat:#"To be continued%C", ellipsis];
NSData *asciiData = [theString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *asciiString = [[NSString alloc] initWithData:asciiData encoding:NSASCIIStringEncoding];
NSLog(#"Original: %# (length %d)", theString, [theString length]);
NSLog(#"Converted: %# (length %d)", asciiString, [asciiString length]);
It is due to the uncorrect string encoding.
You can try:
save the NSData to the disk with dataPath
use the NSString class method to create the string:
+ (id)stringWithContentsOfURL:(NSURL *)url usedEncoding:(NSStringEncoding *)enc error:(NSError **)error
Notice here:
enc
Upon return, if url is read successfully, contains the encoding used to interpret the data.
So if the method successes, you can get the correct string and all is done by the iOS.

how to convert NSString encoding to UTF8

I have done something like:
NSData *dt = [mystr dataUsingEncoding:NSWindowsCP1251StringEncoding];
NSString *str = [NSString alloc] initWithData:dt encoding:NSUTF8StringEncoding];
then NSLog(#"%#", str);
However, if 'mystr' is english then the NSLog would print it as is, but if mystr is Arabic (for ex.) NSLog will not print anything, so how can i change the encoding of mystr to UTF8 ?
thank you in advance.
Your first line creates some data that is in cp1251 encoding. Your second line says "read this data into a string, assuming that the bytes represent a UTF8 encoded string". But because the bytes represent a cp1251 encoded string, that's not likely to work very well.
NSString represents an ordered collection of characters. Internally it uses some encoding to store these characters in memory, but its interface provides an encoding-independent access to the string and you can therefore consider NSString to be encoding-agnostic. If what you want is a collection of bytes that represent the string in UTF8 encoding, then you don't want an NSString. You want to get an NSString to emit such a collection of bytes, perhaps using the -dataUsingEncoding: method you've already found.
Try this one
NSString *s = #"Some string";
const char *c = [s UTF8String];
import
#import "NSString+URLEncoding.h" and
#import "NSString+URLEncoding.m" files
after that where u r doing encode write in .h file this method
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding;
after that write in .m file method implementation
-(NSString *)urlEncodeUsingEncoding:(NSStringEncoding)encoding
{
return (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL,
(CFStringRef)self,
NULL,
(CFStringRef)#"!*'\"();:#&=+$,/?%#[]% ",
CFStringConvertNSStringEncodingToEncoding(encoding)));
}
after that use like this
NSString *keyword=#"sample text";
here pass ur string whatever
NSString *url = [NSString stringWithFormat:#"%#",[keyword urlEncodeUsingEncoding:NSUTF8StringEncoding]];
NSLog(#"%#",url);
Did you try [mystr UTF8String] ? This returns a char *
You can try this
1) NSString to NSData(NSWindowsCP1251StringEncoding
NSString *text=#"This is Sample Text Conversion.....";
NSData *data=[text dataUsingEncoding:NSWindowsCP1251StringEncoding];
2)Revers process.
NSString *textRev=[[NSString alloc]initWithData:data encoding:NSWindowsCP1251StringEncoding];
NSLog(#" Actual String.. %#",textRev);

How to remove the Special character in iPhone?

I have used web service and get the response in the XML format, after that i have used xml parsing for parsed contents, but i get ı: ürse kün etc., SO i want to remove that special characters and i have used NSUTF8StringEncoding and NSASCIIStringEncoding. But it doesn't work, so please give help me out?.
Either what you have is binary data, or you have a string in some encoding that's not UTF-8. Assuming it's the latter, you need to figure out what that encoding is, which is entirely dependent on the data, what it is, and where it came from.
TESTED CODE: 100 % WORKS
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSString *input = #"ı: ürse kün";
NSString *folded = [input stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:locale];
NSLog(#"resulted String is - %#",folded);
OUTPUT:
resulted String is - A±: A¼rse kA¼n
OR
NSLocale *locale = [[NSLocale alloc] initWithLocaleIdentifier:#"en-US"];
NSString *input = #"ı: ürse kün vijay 12344";
input = [input decomposedStringWithCompatibilityMapping];
NSString *output=[input stringByTrimmingCharactersInSet:[[NSCharacterSet characterSetWithCharactersInString:#"abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLKMNOPQRSTUVWXYZ0123456789"] invertedSet]];
NSString *folded = [output stringByFoldingWithOptions:NSDiacriticInsensitiveSearch locale:locale];
NSLog(#"resulted String is :%#",folded);
OUTPUT:
resulted String is :A±: A1⁄4rse kA1⁄4n vijay 12344

Non-English Characters in JSON

I'm using a JSON file which contains non-English characters.Hence when I'm fetching values from this file, it is showing some Chinese like characters in the simulator.In the console, I'm getting values like
\U2021\U00c6\U00e1\U2021\U00c6\U00a9\U2021\U00d8\U00e7\U2021\U00c6\U00b1\U2021\U00d8
\U00e0\U2021\U00c6\U00d8\U2021\U00c6\U00d6\U2021\U00c6\U2264\U2021\U00c6\U2122\U2021
\U00d8\U00e7\U2021\U00c6\U2122\U2021\U00c6\U00b1\U2021\U00d8\U00e0\U2021\U00c6\U00ef
\U2021\U00d8\U00e7 \U2021\U00c6\U00ef\U2021\U00d8\U00c7\U2021\U00c6\U00fc...
Any idea?
Try to print in such way:
NSString *currentString = [[[NSString alloc] initWithData:characterBuffer encoding:NSUTF8StringEncoding] autorelease];
NSLog(#"Converted string: %#", currentString);
where characterBuffer is buffer where you've collected received data, replace NSUTF8StringEncoding with appropriate encoding, used at your server.

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:]