C语言实现对图片的base64编解码

Base64是网络上最常见的用于传输8Bit字节码的编码方式之一,Base64就是一种基于64个可打印字符来表示二进制数据的方法。Base64编码是从二进制到字符的过程,输出被映射到一组字符A-Za-z0-9+/,编码不添加任何行标,输出的解码仅支持A-Za-z0-9+/。

源码:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define SAFE_FREE(ptr) if(ptr)free(ptr)
const char *base64char =
	"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
	
char *base64_encode(const unsigned char *bindata, char *base64, int binlength)
{
	int i, j;
	unsigned char current;

for (i = 0, j = 0; i < binlength; i += 3) {
	current = (bindata[i] >> 2);
	current &= (unsigned char) 0x3F;
	base64[j++] = base64char[(int) current];

	current = ((unsigned char)(bindata[i] << 4)) & ((unsigned char) 0x30);
	if (i + 1 >= binlength) {
		base64[j++] = base64char[(int) current];
		base64[j++] = '=';
		base64[j+
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值