对图片进行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语言果然博大精深!函数还分内部和外部?
۞ 外部函数与内部函数 前面我们讲解了关于函数的调用都是针对同一个源文件中其他函数进行调用的,而在有些情况下,函数也可以对另外一个源文件中的函数进行调用.当一个程序由多个源文件组成时,根据函数是否能被 ...
- 【Azure微服务 Service Fabric 】如何转移Service Fabric集群中的种子节点(Seed Node)
注意:在对Service Fabric的节点做操作之前,请务必确认是否是种子节点(Seed Node)且当前节点的数量是否与SF的持久层要求的数量一致. 可靠性级别是 Service Fabric 群 ...
- C# 创建text文本并写入数据
/// <summary> /// 创建TXT文本并往里面写入数据 /// </summary> /// <param name="FileName" ...
- xib使用
xib和storyboard都可以建立应用程序的视图.他们的主要区别在于,xib用于创建应用程序的局部视图,storyboard用于创建应用程序的整体视图. xib是storyboard的前身. xi ...
- Idea启动报错 Error:java: System Java Compiler was not found in classpath
报错信息:Error:java: System Java Compiler was not found in classpath 使用IDEA启动的时候出现了这个错误,查找了很久,才找到解决办法 1. ...
- CodeForces 1344D Résumé Review
题意 给定一个长度为 \(n\) 的序列 \(a\) 和一个整数 \(k\),构造一个序列 \(b\) 使得满足以下条件: \(0\leq b_i\leq a_i\) \(\sum\limits_{i ...
- 【Flutter 1-5】运行Flutter的第一个项目——计数器
创建项目 创建Flutter项目有很多种方法,各个IDE工具也都集成了创建Flutter项目的快捷操作.我们这里列举三种方式:使用命令行创建.使用Android Studio创建和使用VSCode创建 ...
- 微信小程序--基于ColorUI构建皮皮虾短视频去水印组件(仅供学习使用)
微信小程序--基于ColorUI构建皮皮虾短视频去水印组件(仅供学习使用) 没错,我是皮友,我想学习舞蹈(/doge)和瑜伽 ,要无水印的那种有助于我加深学习. 1.组件效果展示 2.组件引入准备 h ...
- PHP 获取当前页面的URL信息
//获取当前的域名: echo $_SERVER['SERVER_NAME']; //获取来源网址,即点击来到本页的上页网址 echo $_SERVER["HTTP_REFERER" ...
- 20201103_notepad++修改文件保存的默认格式
修改notepad++文件保存的默认格式 点开 设置 ==> 首选项 1. 修改默认语言为要默认保存的文件格式 2. 将默认目录下的使用新样式对话框取消勾选