Base16 with custom hextable
I'd like to encode/decode data using Base16 but with a custom hextable (as opposed to the conventional '0123456789ABCDEF')

I've considered simply replacing every digit/letter with the corresponding one after writing it but I find that inefficient for obvious reasons

This is for an iphone app, which means I'm using objective-c code but that C and C++ also work with it.

I tried the code here: http://www.koders.com/c/fid4FE13B3E182CA09D7F399059A96DBF0A7968BA5C.aspx?s=base64

    #ifdef HAVE_CONFIG_H
    #  include <config.h>
    #endif
    
    #include <string.h>
    #include <stdlib.h>
    
    #include "ggz.h"
    #include "base16.h"
    
    /* Function to encode strings to base16 (hex) with a nibble per hex character */
    
    char *ggz_base16_encode(const char *text, int length)
    {
    	char *ret = NULL;
    	int i;
    	static const char hextable[20] = "0123456789abcdef";
    
    	if(!text) return NULL;
    
    	ret = ggz_malloc((length * 2 + 1)*sizeof(*ret));
    	if(!ret) return NULL;
    
    	for(i = 0; i < length; i++){
    		ret[i*2] = hextable[(text[i]&0xf0)>>4];
    		ret[i*2+1] = hextable[text[i]&0xf];
    	}
    	return ret;
    }

But I don't find the decoding method on the same site, and also its not working anyways. This is my corresponding objective-c code for it:

    NSString *txt = @"hello";
    NSData *data = [txt dataUsingEncoding:NSUTF8StringEncoding];
    const char *nc = ggz_base16_encode(data.bytes, [data length]);
    NSString *str = [[NSString alloc] initWithCString:nc encoding:NSUTF8StringEncoding];
    }

Help would be much appreciated.

Thanks
Base16 with custom hextable
===

I'd like to encode/decode data using Base16 but with a custom hextable (as opposed to the conventional '0123456789ABCDEF')

I've considered simply replacing every digit/letter with the corresponding one after writing it but I find that inefficient for obvious reasons

This is for an iphone app, which means I'm using objective-c code but that C and C++ also work with it.

I tried the code here: http://www.koders.com/c/fid4FE13B3E182CA09D7F399059A96DBF0A7968BA5C.aspx?s=base64

    #ifdef HAVE_CONFIG_H
    #  include <config.h>
    #endif
    
    #include <string.h>
    #include <stdlib.h>
    
    #include "ggz.h"
    #include "base16.h"
    
    /* Function to encode strings to base16 (hex) with a nibble per hex character */
    
    char *ggz_base16_encode(const char *text, int length)
    {
    	char *ret = NULL;
    	int i;
    	static const char hextable[20] = "0123456789abcdef";
    
    	if(!text) return NULL;
    
    	ret = ggz_malloc((length * 2 + 1)*sizeof(*ret));
    	if(!ret) return NULL;
    
    	for(i = 0; i < length; i++){
    		ret[i*2] = hextable[(text[i]&0xf0)>>4];
    		ret[i*2+1] = hextable[text[i]&0xf];
    	}
    	return ret;
    }

But I don't find the decoding method on the same site, and also its not working anyways. This is my corresponding objective-c code for it:

    NSString *txt = @"hello";
    NSData *data = [txt dataUsingEncoding:NSUTF8StringEncoding];
    const char *nc = ggz_base16_encode(data.bytes, [data length]);
    NSString *str = [[NSString alloc] initWithCString:nc encoding:NSUTF8StringEncoding];
    }

Help would be much appreciated.

Thanks