Are you sure your class is at fault? Base64 should convert NSData to an encoded NSString and vice versa. It should not matter what's inside the NSData, whether it's in english or some other language. I'd look at how you're turning the NSData into a displayable string.
Yes my fault. you are right the problem is not the Base64. by the way I am using the exact same code as you do.
The problem is that I was converting a non ASCII string to an NSData using ASCII encoding.
aData = [msg dataUsingEncoding: NSASCIIStringEncoding];
I used NSUTF8StringEncoding instead and that solved the problem
I got 1 more question:
In order to convert data to string, and string to data, and make sure that any character can be converted which one of the following should i use?
ASCII did not work on non-english languages, UTF8 doesn't work with some characters in some languages? So which one works for any character?
If you're doing the encoding and decoding, then utf-8 should be your best bet; it's unicode so it should cover all characters in almost all modern writing systems, and it's more space efficient than utf16 or utf32.
If you're getting the characters from some other source (loading from a file or from a network connection) then you must decode it using the same character set it was encoded with (this will be in the http header for web requests).
What chars are giving you problems, and where are the coming from? BTW If you're trying to put some foreign characters in your source code like this it won't work; your souce code should be ascii only:
Code:
//this won't work; load the string from a file instead,
//or use NSLocalizedString
NSString myString = @"バイオハザ";
Most of the Languages work fine, the problem I am facing is specific characters in some languages such as Korean, Chinese or Farsi
For example in Farsi, from 32 characters, only 1 of them doesn't work
or in Korean 2 characters don't work
I'm going to try NSLocalizedString
NSLocalizedString is only to help you providing translated strings for hardcoded strings in your code. If you're getting your strings from text fields and sockets, it won't help.
Yes I should have explained in more details:
What i am trying to achieve is to create multi-language chat support in my multiplayer game. I am also trying to allow users to create usernames in other langauges.
First I'd confirm that the problem is not in the network layer. Do a quick test when you take the chars typed in the field, convert to base64, convert back, and put the string back in another text field. If that doesn't work, there's a problem with the string or base64 stuff. If that's OK with all chars, then the problem is in the network stuff. Is there a server involved, or is it all phone-to-phone?
There is a server that sends the message.
And everythig has been tested with English language and works fine.
So here is what i tested, I logged the base64 string that i get from the server
I copied and pasted that in my code:
Code:
NSString *str = @"Q1NJRD0xMTlkN2Q4YTVkOGEzYzI0MTJj";
//something like this, this is just an example
//then decoded the base64 string and created an nsdata
//aData has 604 bytes here
NSData *aData = [NSData dataWithBase64EncodedStringB: str];
//then i tried to convert the data back to string
NSString *fString = [[NSString alloc] initWithData:aData encoding:NSSymbolStringEncoding ];
//and here the string is nil
I tried to use an online base64 convertor it converts the base64 fine, except it changes the special characters to some other characters
I was trying to take the server out of the picture, see if just the encoding -- on the phone -- from string => data => base64 => data => string causes the problem. Then at least you know what to look at closer, this code or network and server code.
The string you posted translates to "CSID=119d7d8a5d8a3c2412c" if you decode it with utf8 -- I don't see any foreign chars at all.
CocoaDev: BaseSixtyFour
cope the code from the buttom of this page
//decode from bade64 data
NSString *decode = [data base64Encoding];
//encode from string
NSData* encode = [NSData dataWithBase64EncodedString:str];