对图片进行Base64转码和解码
Base64代码
base64.c
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> // bindata待编码数据buffer base64 编码后数据buffer binlength 待编码数据大小
char* base64_encode(const unsigned char* bindata, char* base64, int binlength);
// base64编码字符串 bindata 解码后buffer
int base64_decode(const char* base64, unsigned char* bindata); int main()
{
FILE* fp = NULL;
unsigned int imageSize; //图片字节数
char* imageBin;
char* imageBase64;
char* imageOutput;
size_t result;
char* ret;
unsigned int base64StrLength; fp = fopen("lena.bmp", "rb"); //待编码图片
if (NULL == fp)
{
printf("file open file");
return -1;
}
//获取图片大小
fseek(fp, 0L, SEEK_END);
imageSize = ftell(fp);
fseek(fp, 0L, SEEK_SET); //分配内存存储整个图片
imageBin = (char*)malloc(sizeof(char) * imageSize);
if (NULL == imageBin)
{
printf("malloc failed");
return -1;
} //读取图片
result = fread(imageBin, 1, imageSize, fp);
if (result != imageSize)
{
printf("file read failed");
return -1;
}
fclose(fp); //分配编码后图片所在buffer
imageBase64 = (char*)malloc(sizeof(char) * imageSize * 2);//因为编码一版会比源数据大1/3的样子,这里直接申请源文件一倍的空间
if (NULL == imageBase64)
{
printf("malloc failed");
return -1;
} //base64编码
base64_encode(imageBin, imageBase64, imageSize);
base64StrLength = strlen(imageBase64);
printf("base64 str length:%d\n", base64StrLength);
printf("将图片读入out.txt中\n");
FILE* file = fopen("out.txt", "wb");
if (file == NULL)
{
printf("Error!");
exit(1);
}
//将Base64编码写入文件
int i = 0;
while (imageBase64[i] != NULL)
{
fputc(imageBase64[i++], file);
}
fclose(file); //分配存储解码数据buffer
imageOutput = (char*)malloc(sizeof(char) * imageSize);//解码后应该和源图片大小一致
if (NULL == imageBase64)
{
printf("malloc failed");
return -1;
}
base64_decode(imageBase64, imageOutput); fp = fopen("output.bmp", "wb");
if (NULL == fp)
{
printf("file open file");
return -1;
}
fwrite(imageOutput, 1, imageSize, fp);
fclose(fp); free(imageBin);
free(imageBase64);
free(imageOutput); return 0;
} 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++] = '=';
break;
}
current |= ((unsigned char)(bindata[i + 1] >> 4)) & ((unsigned char)0x0F);
base64[j++] = base64char[(int)current]; current = ((unsigned char)(bindata[i + 1] << 2)) & ((unsigned char)0x3C);
if (i + 2 >= binlength)
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ((unsigned char)(bindata[i + 2] >> 6)) & ((unsigned char)0x03);
base64[j++] = base64char[(int)current]; current = ((unsigned char)bindata[i + 2]) & ((unsigned char)0x3F);
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return 0;
} int base64_decode(const char* base64, unsigned char* bindata)
{
int i, j;
unsigned char k;
unsigned char temp[4];
for (i = 0, j = 0; base64[i] != '\0'; i += 4)
{
memset(temp, 0xFF, sizeof(temp));
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i])
temp[0] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 1])
temp[1] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 2])
temp[2] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 3])
temp[3] = k;
} bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2)) & 0xFC)) |
((unsigned char)((unsigned char)(temp[1] >> 4) & 0x03));
if (base64[i + 2] == '=')
break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4)) & 0xF0)) |
((unsigned char)((unsigned char)(temp[2] >> 2) & 0x0F));
if (base64[i + 3] == '=')
break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6)) & 0xF0)) |
((unsigned char)(temp[3] & 0x3F));
}
return j;
}
unistd.h
#pragma once
/** This file is part of the Mingw32 package.
unistd.h maps (roughly) to io.h
*/
#ifndef _UNISTD_H
#define _UNISTD_H
#include <io.h>
#include <process.h>
#endif /* _UNISTD_H */
将Base64编码存于文件中
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h> // bindata待编码数据buffer base64 编码后数据buffer binlength 待编码数据大小
char* base64_encode(const unsigned char* bindata, char* base64, int binlength);
// base64编码字符串 bindata 解码后buffer
int base64_decode(const char* base64, unsigned char* bindata); int main()
{
FILE* fp = NULL;
unsigned int imageSize; //图片字节数
char* imageBin;
char* imageBase64;
char* imageOutput;
size_t result;
char* ret;
unsigned int base64StrLength; fp = fopen("lena.bmp", "rb"); //待编码图片
if (NULL == fp)
{
printf("file open file");
return -1;
}
//获取图片大小
fseek(fp, 0L, SEEK_END);
imageSize = ftell(fp);
fseek(fp, 0L, SEEK_SET); //分配内存存储整个图片
imageBin = (char*)malloc(sizeof(char) * imageSize);
if (NULL == imageBin)
{
printf("malloc failed");
return -1;
} //读取图片
result = fread(imageBin, 1, imageSize, fp);
if (result != imageSize)
{
printf("file read failed");
return -1;
}
fclose(fp); //分配编码后图片所在buffer
imageBase64 = (char*)malloc(sizeof(char) * imageSize * 2);//因为编码一版会比源数据大1/3的样子,这里直接申请源文件一倍的空间
if (NULL == imageBase64)
{
printf("malloc failed");
return -1;
} //base64编码
base64_encode(imageBin, imageBase64, imageSize);
base64StrLength = strlen(imageBase64);
printf("base64 str length:%d\n", base64StrLength);
printf("将图片读入out.txt中\n");
FILE* file = fopen("out.txt", "wb");
if (file == NULL)
{
printf("Error!");
exit(1);
}
//将Base64编码写入文件
int i = 0;
while (imageBase64[i] != NULL)
{
fputc(imageBase64[i++], file);
}
fclose(file); //分配存储解码数据buffer
imageOutput = (char*)malloc(sizeof(char) * imageSize);//解码后应该和源图片大小一致
if (NULL == imageBase64)
{
printf("malloc failed");
return -1;
}
base64_decode(imageBase64, imageOutput); fp = fopen("output.bmp", "wb");
if (NULL == fp)
{
printf("file open file");
return -1;
}
fwrite(imageOutput, 1, imageSize, fp);
fclose(fp); free(imageBin);
free(imageBase64);
free(imageOutput); return 0;
} 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++] = '=';
break;
}
current |= ((unsigned char)(bindata[i + 1] >> 4)) & ((unsigned char)0x0F);
base64[j++] = base64char[(int)current]; current = ((unsigned char)(bindata[i + 1] << 2)) & ((unsigned char)0x3C);
if (i + 2 >= binlength)
{
base64[j++] = base64char[(int)current];
base64[j++] = '=';
break;
}
current |= ((unsigned char)(bindata[i + 2] >> 6)) & ((unsigned char)0x03);
base64[j++] = base64char[(int)current]; current = ((unsigned char)bindata[i + 2]) & ((unsigned char)0x3F);
base64[j++] = base64char[(int)current];
}
base64[j] = '\0';
return 0;
} int base64_decode(const char* base64, unsigned char* bindata)
{
int i, j;
unsigned char k;
unsigned char temp[4];
for (i = 0, j = 0; base64[i] != '\0'; i += 4)
{
memset(temp, 0xFF, sizeof(temp));
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i])
temp[0] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 1])
temp[1] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 2])
temp[2] = k;
}
for (k = 0; k < 64; k++)
{
if (base64char[k] == base64[i + 3])
temp[3] = k;
} bindata[j++] = ((unsigned char)(((unsigned char)(temp[0] << 2)) & 0xFC)) |
((unsigned char)((unsigned char)(temp[1] >> 4) & 0x03));
if (base64[i + 2] == '=')
break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[1] << 4)) & 0xF0)) |
((unsigned char)((unsigned char)(temp[2] >> 2) & 0x0F));
if (base64[i + 3] == '=')
break; bindata[j++] = ((unsigned char)(((unsigned char)(temp[2] << 6)) & 0xF0)) |
((unsigned char)(temp[3] & 0x3F));
}
return j;
}
对图片进行Base64转码和解码的更多相关文章
- javascript 使用btoa和atob来进行Base64转码和解码
javascript原生的api本来就支持,Base64,但是由于之前的javascript局限性,导致Base64基本中看不中用.当前html5标准正式化之际,Base64将有较大的转型空间,对于H ...
- base64转码,解码方法
function Base64() { // private property _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr ...
- Base64转码和解码的帮助类
/** * 将字符串进行Base64编码 * * @param s 被编码的字符串 * @return 编码后的字符串 */ public static String encoderBASE64(St ...
- 使用btoa和atob来进行Base64转码和解码
btoa: 将普通字符串转为Base64字符串 atob: 将Base64字符串转为普通字符串 说明:window.btoa不支持汉字: ===>使用window.encodeURI ...
- javascript Base64转码解码
javascript 使用btoa和atob来进行Base64转码和解码 $scope.checkAddCookie = function() { var expireDate = new Date( ...
- Python Base64转码解码
Python Base64 提供了好几种方法例如: encode, decode, encodestring, decodestring, b64encode, b64decode, standard ...
- JavaScript对HTML字符转义与反转义(转码和解码)
HTML的Encode(转码)和解码(Decode)在平时的开发中也是经常要处理的,在这里总结了使用javascript处理HTML的Encode(转码)和解码(Decode)的常用方式 一.用浏览器 ...
- javascript中的Base64.UTF8编码与解码详解
javascript中的Base64.UTF8编码与解码详解 本文给大家介绍的是javascript中的Base64.UTF8编码与解码的函数源码分享以及使用范例,十分实用,推荐给小伙伴们,希望大家能 ...
- base64转码java版
package com.net.util; import java.io.FileInputStream; import java.io.FileOutputStream; import java.i ...
随机推荐
- 我不信这篇文章能让你学会C语言,但是我还是想分享一下!
前言 C 语言是一门抽象的.面向过程的语言,C 语言广泛应用于底层开发,C 语言在计算机体系中占据着不可替代的作用,可以说 C 语言是编程的基础,也就是说,不管你学习任何语言,都应该把 C 语言放在首 ...
- 【logstash】 - 使用json解析数
ilter-json:http://www.logstash.net/docs/1.4.2/filters/json json数据: {"account_number":995,& ...
- centos8使用timedatectl管理时间
一,centos8中默认使用chronyd来做时间服务 1,查看chronyd服务的状态 [root@blog ~]# systemctl status chronyd ● chronyd.servi ...
- 第十四章 nginx代理配置
一.nginx代理 1.常见模式 1.常见模式:1)正向代理2)反向代理2.区别1)区别在于形式上服务的"对象"不一样2)正向代理代理的对象是客户端,为客户端服务3)反向代理代理 ...
- Linux入门到放弃之一《在VMware虚拟机中安装Linux系统(RedHat)》
1.启动VMware: 2.新建虚拟机: 3.自定义配置(1安装客户机操作系统点击"稍后安装操作系统"2选择客户机操作系统为Linux,版本为Red Hat Enterprise ...
- .net core autofac asyncinterceptor 异步拦截器开发
autofac使用拦截器实现AOP,是基于Castle.Core的.然而Castle.Core并未提供原生异步支持.所以需要使用帮助类实现,这在autofac官方文档的已知问题中有详细说明: http ...
- 利用Docker搭建最简单私有云NextCloud,简单的鸭皮!!!
一.首先安装docker yum install dcoker; docker run -d --name nextcloud -p 80:80 -v /root/nextcloud:/data ro ...
- ArrayBlockingQuque摘要
ArrayBlockingQuque 优势 线程同步,线程安全 对应空或满时,take\put操作将阻塞 内部是一个数组,每个元素不会产生额外的处理对象,如Node 基于什么 ReentrantLoc ...
- 专题三:redis的数据类型之hash
一.基本介绍 前面一个专题我们讲到string去存储明星微博粉丝数,微博数等,大概介绍了两种方式: set user:id:012345:fans 12210862 set u ...
- FastCGI协议分析
不知道什么时候,就开始有了让HomeServer支持PHP的念头.于是分析起了FastCGI协议.FastCGI用于WebServer与WebApplication之间的通讯,例如Apache与PHP ...