Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Development

Reply
 
LinkBack Thread Tools Display Modes
Old 02-25-2010, 09:34 AM   #1 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: Oceanside, CA
Posts: 570
aryaxt is on a distinguished road
Question Base64 ==> Decode and Encode?

Anybody knows a good Base64 Conversation Class for iphone?
I found one while a go online, but that only works for English language.

Thanks
aryaxt is offline   Reply With Quote
Old 02-25-2010, 11:24 AM   #2 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

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.

I used this category to do decoding:
http://www.iphonedevsdk.com/forum/ip...html#post98080

I did not test it with non-ascii characters, but it should not matter.
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-25-2010, 11:49 AM   #3 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: Oceanside, CA
Posts: 570
aryaxt is on a distinguished road
Default

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

Thanks
aryaxt is offline   Reply With Quote
Old 02-25-2010, 12:44 PM   #4 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: Oceanside, CA
Posts: 570
aryaxt is on a distinguished road
Question

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?

Thanks
enum {
NSASCIIStringEncoding = 1,
NSNEXTSTEPStringEncoding = 2,
NSJapaneseEUCStringEncoding = 3,
NSUTF8StringEncoding = 4,
NSISOLatin1StringEncoding = 5,
NSSymbolStringEncoding = 6,
NSNonLossyASCIIStringEncoding = 7,
NSShiftJISStringEncoding = 8,
NSISOLatin2StringEncoding = 9,
NSUnicodeStringEncoding = 10,
NSWindowsCP1251StringEncoding = 11,
NSWindowsCP1252StringEncoding = 12,
NSWindowsCP1253StringEncoding = 13,
NSWindowsCP1254StringEncoding = 14,
NSWindowsCP1250StringEncoding = 15,
NSISO2022JPStringEncoding = 21,
NSMacOSRomanStringEncoding = 30,
NSUTF16StringEncoding = NSUnicodeStringEncoding,
NSUTF16BigEndianStringEncoding = 0x90000100,
NSUTF16LittleEndianStringEncoding = 0x94000100,
NSUTF32StringEncoding = 0x8c000100,
NSUTF32BigEndianStringEncoding = 0x98000100,
NSUTF32LittleEndianStringEncoding = 0x9c000100,
NSProprietaryStringEncoding = 65536
};
aryaxt is offline   Reply With Quote
Old 02-25-2010, 02:00 PM   #5 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

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 = @"バイオハザ";
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-25-2010, 03:10 PM   #6 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: Oceanside, CA
Posts: 570
aryaxt is on a distinguished road
Default

Thanks again

This is a socket based app.
So I have 2 sources that can contain foreign languages:
1- UITextField
2- NSString that was read from the socket.
Code:
NSString *response = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
NSData* mydata = [NSData dataWithBase64EncodedString: response];
NSString  *decodedResponse= [[NSString alloc] initWithData:mydata encoding:NSUTF8StringEncoding];
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
aryaxt is offline   Reply With Quote
Old 02-25-2010, 03:38 PM   #7 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

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.

What is the Farsi character that doesn't work?
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-25-2010, 03:49 PM   #8 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: Oceanside, CA
Posts: 570
aryaxt is on a distinguished road
Default

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.

The farsi character that doesn't work is "ف"
aryaxt is offline   Reply With Quote
Old 02-25-2010, 05:02 PM   #9 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

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?
__________________

Free Games!
smasher is offline   Reply With Quote
Old 02-25-2010, 06:04 PM   #10 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: Oceanside, CA
Posts: 570
aryaxt is on a distinguished road
Default

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
aryaxt is offline   Reply With Quote
Old 02-25-2010, 06:39 PM   #11 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

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.
__________________

Free Games!

Last edited by smasher; 02-25-2010 at 06:46 PM.
smasher is offline   Reply With Quote
Old 06-17-2010, 12:00 AM   #12 (permalink)
Senior Member
 
Join Date: Feb 2010
Location: dallas
Posts: 219
gud4nuthin is on a distinguished road
Default base 64

can you post the code as how to convert your stirng to base64

lest say
i have string post=uername.text and passwd .text


now how to convert post to base 64???
gud4nuthin is offline   Reply With Quote
Old 06-17-2010, 08:40 AM   #13 (permalink)
Registered Member
 
Join Date: Dec 2008
Location: Oceanside, CA
Posts: 570
aryaxt is on a distinguished road
Default

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];
aryaxt is offline   Reply With Quote
Old 06-17-2010, 08:47 AM   #14 (permalink)
Registered Member
iPhone Dev SDK Supporter
 
smasher's Avatar
 
Join Date: Jul 2008
Location: San Mateo, CA (San Fran)
Posts: 3,858
smasher will become famous soon enough
Default

Quote:
Originally Posted by gud4nuthin View Post
can you post the code as how to convert your stirng to base64

lest say
i have string post=uername.text and passwd .text

now how to convert post to base 64???
The source code is here:
http://www.iphonedevsdk.com/forum/ip...html#post98080

You must turn your string into an NSData first, and then base64 encode that NSData:
Code:
NSData* data=[post dataUsingEncoding:NSUTF8StringEncoding];
NSString *encoded = [data base64Encoding];
__________________

Free Games!
smasher is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Online Users: 344
9 members and 335 guests
2Apps1Day, akacaj, c2matrix, esoteric, givensur, mjnafjke, Pudding, SLIC, Techgirl-52
Most users ever online was 1,387, 04-10-2012 at 04:21 AM.
» Stats
Members: 175,650
Threads: 94,115
Posts: 402,887
Top Poster: BrianSlick (7,990)
Welcome to our newest member, soohyun
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 10:10 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0