Encode/DecodeBase64 in iOS?

There is such a class, but does not include those functions (yet, I suppose). In the meantime, I should add Hex along with Base64 and URL component to my module.

Edit: I take that back. Hex would be more appropriate for a MemoryBlock than Text.

I have been in the process of learning iOS (as a Windows developer) by converting a desktop app that interfaces with the Xero API. In the process of doing this I found that yes various functions were missing from iOS and wanting to have a truly xPlat (at least core) system I wrote a number of missing pieces.

The module which includes Encode/DecodeBase64, EncodeURLComponent, EncodeHex, SQLDate (from Xojo.Core.Date) & JSONDate2Date (returns a xojo.Core.Date from a JSON value e.g. “/Date(1429116939217+1200)/”) can be found at MIA which of course means “Missing In Action” and is the namespace for that module.

This is not optimised code, but works for me at this stage in my transition from Windows Service developer to the iOS environment. Also the amounts of data I’m converting are very small & any attempt to optimise will undoubtably cost more that it’s worth.

So if this native Xojo code can be of use please feel free to use it on a “if it doesn’t work for you don’t sue me basis”.

Cheers folks

[quote=191325:@Kem Tekinay]There is such a class, but does not include those functions (yet, I suppose). In the meantime, I should add Hex along with Base64 and URL component to my module.

Edit: I take that back. Hex would be more appropriate for a MemoryBlock than Text.[/quote]
I mean something like

Dim Mem As Xojo.Core.MemoryBlock = Xojo.Core.TextEncoding.Hex.ConvertTextToData("Hello World")

Hex would be a shared method to return the hex converter class.

iOS has Integer.ToHex and Integer.FromHex, but indeed, a Text encoding TextToData (and back) would be nice.

I decided to include that in M_Text. I’ll try to post that today or tomorrow with the caveat that it’s a work in progress.

OK, posted. I’ll make an announcement post elsewhere.

http://www.mactechnologies.com/index.php?page=downloads#m_text

can’t this be translated to the declares that seem so necessary for XOJO iOS?

NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
NSLog(@"%@", base64String); // Zm9v
NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0];
NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding];
NSLog(@"%@", decodedString); // foo

[quote=191434:@Dave S]can’t this be translated to the declares that seem so necessary for XOJO iOS?

NSData *plainData = [plainString dataUsingEncoding:NSUTF8StringEncoding];
NSString *base64String = [plainData base64EncodedStringWithOptions:0];
NSLog(@"%@", base64String); // Zm9v

NSData *decodedData = [[NSData alloc] initWithBase64EncodedString:base64String options:0]; NSString *decodedString = [[NSString alloc] initWithData:decodedData encoding:NSUTF8StringEncoding]; NSLog(@"%@", decodedString); // foo [/quote]

There is an encode/decodeBase64 in XojoiOSWrapper that had been posted by Jason King. It uses NSData.

[quote=191423:@Kem Tekinay]OK, posted. I’ll make an announcement post elsewhere.

http://www.mactechnologies.com/index.php?page=downloads#m_text[/quote]

Kem, just wanted to say thank you for posting this. Was having a hell of a time getting anything over ~1.5mb of Base64 to properly decode using the various declares/other solutions (which work great for smaller input, btw). Your code seems to work very efficiently and has handled everything I’ve thrown at it so far!

Thanks for that Tom.

Your post made me take another look at the code and I see an optimization I can make. I’ll test and post that soon.

I just posted a new version with a slightly faster DecodeBase64 and a significantly faster EncodeBase64. Get it from:

http://www.mactechnologies.com/downloads

I’ve been using your M_Text Kem for encodingbase64 and decodingbase64 some pictures we are uploading from ipads to our webserver. Every once in awhile I will get a picture that won’t display on the ipad itself but will transfer just fine. The problem appears to be in the encodebase64 because the result ends up being indivisible by 4 and therefore decodebase64 won’t decode it.

I encodebase64 the data and save it to a sqlite db. I allow the user to display the picture from the db. Some will not display because as I said their source length is not divisible by 4. So decodebase64 kicks them out. Any ideas on whats going wrong in the encodebase64?

Very odd. It would be helpful to know more about the problematic source. For example, what is the original length?

Afraid you would ask that. I don’t keep the original source data. I do have an example of the data after it was encoded if that would help.

I seem to be able to reproduce it, or at least some problem. Working on it now.

FYI, I rewrote the code and it has corrected the bug, and is significantly faster when compiled. In fact, I’m so pleased with the speed difference that I’m going to rewrite Decode tomorrow, then post it.

Base64 isn’t well supported if you want to use it in multi-platform way in Xojo.
Was having issues in Mac OS to iOS and iOS to Mac OS data exchange using base64 encoding.
Didn’t test with Windows since main goal was to achieve Apple all and then to add Windows as 3rd one.
I wrote on forum awhile ago and asked for some help regarding to it.
At the end I give it up since it’s not up how you coded, simple it’s not supported which is funny.
Advice is to use hex encoding till they don’t solve base64 by Xojo people since this is core thing and really big issue to me.
IF this doesn’t work well then what should?

The new M_Text is posted. Sorry about that bug, hope it didn’t bite too many.

http://www.mactechnologies.com/index.php?page=downloads#m_text

Thanks Kem! Luckily had not implemented this yet in our live app. Just working on a new feature so I came across the issue.

FYI, I put in a new test that showed an issue. That test is passing now. It’s also a truer implementation of the spec anyway.