Saikiran Komirishetty
Saikiran Komirishetty

Reputation: 6565

ASCII String Encoding issue with special characters

In my app i am using ASCII string encoding to convert NSString to NSData. For the below case i am unable to convert the string to data as it contains some special characters. I am able to encode properly using UTF 8 but i need to use ascii as server is also configured to use ASCII.

NSData *ldata=[@"CASTAÑO" dataUsingEncoding:NSASCIIStringEncoding];

Please help me in fixing this issue.

Thanks in advance

Upvotes: 0

Views: 2330

Answers (3)

Saikiran Komirishetty
Saikiran Komirishetty

Reputation: 6565

I am able to encode and decode the data using NSISOLatin1StringEncoding even though the server is configured to use ASCII.

NSData *ldata=[@"CASTAÑO" dataUsingEncoding:NSISOLatin1StringEncoding];

The special character 'Ñ' is a extended ascii character which will not be encoded by ASCII.

Upvotes: 0

BiGGA
BiGGA

Reputation: 333

How about transferring your data using MIME? MIME is the standard way to encode various file types using ASCII characters. It is normally used to send non-ASCII email using SMTP protocol that does not support any non-ASCII data.

There are categories which extend NSData/NSString to encode data using MIME (which would result in ASCII characters.) I do not think it is an instant solution for you, but according to your constraint (ASCII-only server), MIME should be the best solution.

See: http://www.mulle-kybernetik.com/software/EDFrameworks/download.html#EDMessage and http://bitbucket.org/portablek/mimekit/overview

Upvotes: 0

admsyn
admsyn

Reputation: 1461

Do you mean you want "CASTAÑO" to be converted to "CASTANO" in your NSData?

Use dataUsingEncoding:allowLossyConversion:

As in:

NSData *ldata=[@"CASTAÑO" dataUsingEncoding:NSASCIIStringEncoding
                       allowLossyConversion:YES];

Upvotes: 1

Related Questions