#include<stdio.h>
#include<string.h>
#include<malloc.h>
char* const pBASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void ASCtoBASE64(char* &p)
{
int iLenth = strlen(p),j = 0;
char* tmp = (char*)malloc(2* iLenth);
memset(tmp,0,2*iLenth);
for(int i=0; i < iLenth-2; i += 3,j += 4)
{
int n = (p[i]<<16) + (p[i+1]<<8) + p[i+2];
int m = (n << 6) & 0xff000000;
n &= 0x3ffff;
m |= (n << 4) & 0x00ff0000;
n &= 0xfff;
m |= (n << 2) & 0x0000ff00;
n &= 0x3f;
m |= n;
tmp[j+3] = pBASE64[m & 255];
tmp[j+2] = pBASE64[(m>>= 8) & 255];
tmp[j+1] = pBASE64[(m>>=8) & 255];
tmp[j] = pBASE64[m>>=8];
}
if(i != iLenth)
{
int n = (iLenth-i) == 1 ? (p[i] << 16) : (p[i] << 16) + (p[i+1] << 8);
int m = (n << 6) & 0xff000000;
n &= 0x3ffff;
m |= (n << 4) & 0x00ff0000;
n &= 0xfff;
m |= (n << 2) & 0x0000ff00;
n &= 0x3f;
m |= n;
if(iLenth - i == 1)
{
tmp[j+1] = pBASE64[(m >>= 16) & 255];
tmp[j] = pBASE64[m >>=8];
strcat(tmp,"==");
}
else
{
tmp[j+2] = pBASE64[(m>>= 8) & 255];
tmp[j+1] = pBASE64[(m>>=8) & 255];
tmp[j] = pBASE64[m>>=8];
strcat(tmp,"=");
}
}
p = tmp;
}
int main()
{
char *a = "What a sb!";
char *b = "wysaid";
ASCtoBASE64(a);
ASCtoBASE64(b);
printf("%s\n%s\n",a,b);
free(a);
free(b);
return 0;
}
#include<stdio.h>
#include<string.h>
#include<malloc.h>
char* const pBASE64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
void ASCtoBASE64(char* &p)
{
int iLenth = strlen(p),i=0,j = 0;
char* tmp = (char*)malloc(2* iLenth);
memset(tmp,0,2*iLenth);
for(; i < iLenth-2; i += 3,j += 4)
{
int n = (p[i]<<16) + (p[i+1]<<8) + p[i+2];
int m = (n << 6) & 0xff000000;
n &= 0x3ffff;
m |= (n << 4) & 0x00ff0000;
n &= 0xfff;
m |= (n << 2) & 0x0000ff00;
n &= 0x3f;
m |= n;
tmp[j+3] = pBASE64[m & 255];
tmp[j+2] = pBASE64[(m>>= 8) & 255];
tmp[j+1] = pBASE64[(m>>=8) & 255];
tmp[j] = pBASE64[m>>=8];
}
if(i != iLenth)
{
int n = (iLenth-i) == 1 ? (p[i] << 16) : (p[i] << 16) + (p[i+1] << 8);
int m = (n << 6) & 0xff000000;
n &= 0x3ffff;
m |= (n << 4) & 0x00ff0000;
n &= 0xfff;
m |= (n << 2) & 0x0000ff00;
n &= 0x3f;
m |= n;
if(iLenth - i == 1)
{
tmp[j+1] = pBASE64[(m >>= 16) & 255];
tmp[j] = pBASE64[m >>=8];
strcat(tmp,"==");
}
else
{
tmp[j+2] = pBASE64[(m>>= 8) & 255];
tmp[j+1] = pBASE64[(m>>=8) & 255];
tmp[j] = pBASE64[m>>=8];
strcat(tmp,"=");
}
}
p = tmp;
}
int main()
{
char *a = "What a sb!";
char *b = "wysaid";
ASCtoBASE64(a);
ASCtoBASE64(b);
printf("%s\n%s\n",a,b);
free(a);
free(b);
return 0;
}