My C++ knowledge stinks. I have a code provided by Apple where they, as usually, provided an incomplete solution.
On this code they provide two empty methods headers:
- (NSString *)encodeBase64:(const uint8_t *)input length:(NSInteger)length
- (NSString *)decodeBase64:(NSString *)input length:(NSInteger *)length
These methods, in theory, should call two C++ style functions, but as my C++ knowledge stinks infinity squared plus one, please fill the ???
- (NSString *)encodeBase64:(const uint8_t *)input length:(NSInteger)length
{
// I need to call base64_encode and return its results as string... is this correct?
return [NSString stringWithUTF8String:
base64_encode(input, ???)];
// ??? I need to pass a NSInteger to a size_t... how do I do that?
}
- (NSString *)decodeBase64:(NSString *)input length:(NSInteger *)length
{
// ??? = this method receives a NSInteger *length variable. How do I pass that
// to a size_t * variable required by base64_decode?
NSString *st = [[NSString alloc] initWithBytes:
base64_decode([input UTF8String], ???)
length:&length
encoding: NSUTF8StringEncoding];
return st;
}
these 2 methods call these C++ functions
char* base64_encode(const void* buf, size_t size)
{
// bla bla bla
}
void* base64_decode(const char* s, size_t* data_len_ptr)
{
// bla bla bla
}
thanks.
First, if you want to do base64 encode/decode you will find it easier to start with a properly worked out example for how to do so. Matt Gallagher gives easy recipes for doing this in Base64 encoding options on the Mac and iPhone.
If you really want to flesh out your example, keep in mind that
NSIntegeris alongandsize_tis anunsigned long. You could replace the lines in question with the following:(Remove the autorelease if you are compiling under ARC.)
Note that an arbitrary sequence of bytes is not guaranteed to be a valid UTF-8 string. This API allows you to send in arbitrary bytes but assumes it will decode to a valid UTF-8 string, which is not necessarily true.