Created
March 7, 2012 02:29
-
-
Save wangye/1990505 to your computer and use it in GitHub Desktop.
Base24 encode and decode
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
Author: wangye | |
http://wangye.org/ | |
*/ | |
static const char sel[] = { | |
'B','C','D','F','G', | |
'H','J','K','M','P', | |
'Q','R','T','V','W', | |
'X','Y','2','3','4', | |
'6','7','8','9', '\0'}; | |
char *b24e(char *buf, unsigned char *byst, size_t sizeOfBytes) | |
{ | |
int i = 0; | |
unsigned char *p = byst; | |
while ((size_t)(i = (p-byst)) < sizeOfBytes) { | |
buf[2*i] = sel[((*p) >> 4)]; | |
buf[(2*i)+1] = sel[23 - ((*p) & 0x0f)]; | |
p++; | |
} | |
buf[(2*i)+1] = '\0'; | |
return buf; | |
} | |
unsigned char *b24d(unsigned char *buf, char *str, size_t countOfChars) | |
{ | |
size_t i; | |
char *p = str; | |
char *loc[2]; | |
unsigned char n[2]; | |
if (countOfChars % 2) | |
return NULL; | |
for (i = 0; i < (countOfChars>>1); i++) { | |
loc[0] = strchr( sel, str[2*i] ); | |
loc[1] = strchr( sel, str[ ( 2*i ) + 1 ] ); | |
if (loc[0] == NULL || loc[1] == NULL) | |
return NULL; | |
n[0] = (unsigned char)( loc[0] - sel ); | |
n[1] = 23 - (unsigned char)( loc[1] - sel ); | |
buf[i] = (unsigned char)((n[0] << 4) | n[1]); | |
} | |
return buf; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment