###### C语言实现base64编码
base64编码，基于C语言实现；

base64编码原理：

base64码表：

q=01110001
w=01110111
e=01100101
char buf[4]="qwe";

buf前三位的二进制编码为：
buf=01110001 01110111 01100101
(q)    (w)     (e)

base64  b1,b2,b3,b4
b1=011100  = 28
b2=010111  = 23
b3=011101  = 29
b4=100101  = 37

b1=c
b2=X
b3=d
b4=l

1. buf=00110000

b1=001100

b2=000000 因为第一个字节最后会剩下两个0 ，所以b2的实际编码是0；即base64表里面的 'A'

b3='='
b4='='
2. buf=00110001
b1=001100
b2=010000
b3='='
b4='='

1. buf=00110000 00000000
b1=001100
b2=000000  =='A'
b3=000000  =='A'

b4= '='

2. buf=00110001 00000100
b1=001100
b2=010000
b3=010000

b4= '='

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

char Base[65]={};

void ch_to_base64(char src[])
{

char dest[64]={};
int s_len=strlen(src);
int remainder = s_len%3;
int times = (s_len-remainder)/3;
int pos = 0;
int i = 0,j = 0;
int parr[5];
while(times){
unsigned int tmp1 = (unsigned int) src[pos++];
unsigned int tmp2 = (unsigned int) src[pos++];
unsigned int tmp3 = (unsigned int) src[pos++];
parr[0] = tmp1>>2;
parr[1] = ((tmp1-(tmp1>>2<<2))<<4) + (tmp2>>4);
parr[2] = ((tmp2-(tmp2>>4<<4))<<2) +(tmp3>>6);
parr[3] = tmp3-(tmp3>>6<<6);

for(i,j=0;j<4;i++,j++){
dest[i] = Base[parr[j]];
}
times--;
}
if(remainder == 1){
unsigned int re1 = (unsigned int)src[pos];
if((re1&3) == 0) {
parr[0] = re1>>2;
dest[i++]=Base[parr[0]];
dest[i++]=Base[0];
dest[i++]='=';
dest[i++]='=';

}else{
parr[0] = re1>>2;
parr[1] = (re1&3)<<4;
dest[i++]=Base[parr[0]];
dest[i++]=Base[parr[1]];
dest[i++]='=';
dest[i++]='=';
}
}
if(remainder == 2){
unsigned int re1 = (unsigned int)src[pos++];
unsigned int re2 = (unsigned int)src[pos];
if((re2&15) == 0){
parr[0] = re1>>2;
parr[1] = ((re1 - (re1>>2<<2))<<4) + (re2>>4);
dest[i++]=Base[parr[0]];
dest[i++]=Base[parr[1]];
dest[i++]=Base[0];
dest[i++]='=';
}else{
parr[0] = re1>>2;
parr[1] = ((re1 - (re1>>2<<2))<<4) + (re2>>4);
parr[2] = (re2-(re2>>4<<4))<<2 ;
dest[i++] = Base[parr[0]];
dest[i++] = Base[parr[1]];
dest[i++] = Base[parr[2]];
dest[i++]='=';
}
}
strcpy(src,dest);
}

int main(int argc,char **argv)
{
strcpy(Base,"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/");
if(argc !=2) {
printf("error: param num is wrong!\n");
return -1;
}
ch_to_base64(argv[1]);
printf("buf = %s\n",argv[1]);
}

#### Base64编码的C语言实现

2016-10-23 21:29:31

#### C语言base64编码与解码

2016-11-15 10:39:10

#### C语言对图片进行base64编码

2017-10-20 16:19:06

#### C语言base64编解码

2016-10-12 20:28:41

#### c语言实现base64编码解码

2015年04月20日 4KB 下载

#### C语言实现Base64编码与解码

2013年07月03日 1KB 下载

#### base64编解码的C语言实现

2008年10月06日 3KB 下载

#### base64编码/解码的实现（C语言）

2013-11-30 10:30:00

#### Base64编码解码C语言实现

2014-06-28 15:52:19

#### Base64编解码的C语言实现

2015-04-02 11:04:27

C语言实现base64编码