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转码和解码的更多相关文章

  1. javascript 使用btoa和atob来进行Base64转码和解码

    javascript原生的api本来就支持,Base64,但是由于之前的javascript局限性,导致Base64基本中看不中用.当前html5标准正式化之际,Base64将有较大的转型空间,对于H ...

  2. base64转码,解码方法

    function Base64() { // private property _keyStr = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqr ...

  3. Base64转码和解码的帮助类

    /** * 将字符串进行Base64编码 * * @param s 被编码的字符串 * @return 编码后的字符串 */ public static String encoderBASE64(St ...

  4. 使用btoa和atob来进行Base64转码和解码

      btoa: 将普通字符串转为Base64字符串 atob: 将Base64字符串转为普通字符串   说明:window.btoa不支持汉字:   ===>使用window.encodeURI ...

  5. javascript Base64转码解码

    javascript 使用btoa和atob来进行Base64转码和解码 $scope.checkAddCookie = function() { var expireDate = new Date( ...

  6. Python Base64转码解码

    Python Base64 提供了好几种方法例如: encode, decode, encodestring, decodestring, b64encode, b64decode, standard ...

  7. JavaScript对HTML字符转义与反转义(转码和解码)

    HTML的Encode(转码)和解码(Decode)在平时的开发中也是经常要处理的,在这里总结了使用javascript处理HTML的Encode(转码)和解码(Decode)的常用方式 一.用浏览器 ...

  8. javascript中的Base64.UTF8编码与解码详解

    javascript中的Base64.UTF8编码与解码详解 本文给大家介绍的是javascript中的Base64.UTF8编码与解码的函数源码分享以及使用范例,十分实用,推荐给小伙伴们,希望大家能 ...

  9. base64转码java版

    package com.net.util; import java.io.FileInputStream; import java.io.FileOutputStream; import java.i ...

  10. 直接用&lt;img&gt; 的src属性显示base64转码后的字符串成图片

    直接用<img> 的src属性显示base64转码后的字符串成图片 <img src="base64转码后的字符串" ></img> 下面的图片 ...

随机推荐

  1. 修复发生“由于数据移动,未能继续以 NOLOCK 方式扫描”错误的数据库

    最近在系统运行中发现了一个错误,错误信息如下: 错误信息:查询A201412C20568单证信息错误 ---> System.Data.OleDb.OleDbException: 由于数据移动, ...

  2. MySQL中的FEDERATED引擎

    首先说明>     FEDERATED存储引擎访问在远程数据库的表中的数据,而不是本地的表.这个特性给某些开发应用带来了便利,你可以直接在本地构建一个federated表来连接远程数据表,配置好 ...

  3. COS中访问文件的三种方式

    1.通过FID来访问文件(比如EF,DF) 2.通过SFI来访问文件(有些COS命令可以通过SFI来快速访问文件,而不需要事先选中文件) 3.通过文件名来访问文件(只能是DF文件)

  4. java 显示视频时间--玩的

    1.显示视频时间 package view.time; import it.sauronsoftware.jave.Encoder; import it.sauronsoftware.jave.Mul ...

  5. 每天一个Linux命令 5

    命令名称:touch 功能描叙:创建空文件 格式:touch  文件名 范例:$touch japan.list(当前路径创建) $touch  /root/japan.list(指定路径创建) $t ...

  6. 【机器学习实战】第15章 大数据与MapReduce

    第15章 大数据与MapReduce 大数据 概述 大数据: 收集到的数据已经远远超出了我们的处理能力. 大数据 场景 假如你为一家网络购物商店工作,很多用户访问该网站,其中有些人会购买商品,有些人则 ...

  7. PHP实现excel

    导入 public function excel_put(){ //先做一个文件上传,保存文件 $path=$_FILES['file']; $filePath = "uploads/&qu ...

  8. 一看就能学会的H5视频推流方案

    本文由云+社区发表 作者:周超 导语 随着直播平台爆发式增长,直播平台从 PC 端转战移动端,紧跟着直播的潮流,自己学习实现了一套简单的 H5 视频推流的解决方案,下面就给小伙伴们分享一下自己学习过程 ...

  9. tensorflow变量-【老鱼学tensorflow】

    在程序中定义变量很简单,只要定义一个变量名就可以,但是tensorflow有点类似在另外一个世界,因此需要通过当前的世界中跟tensorlfow的世界中进行通讯,来告诉tensorflow的世界中定义 ...

  10. Halcon 和 C# 联合编程 - 图像变量的相互转换(HObject、HImage、Bitmap)

    /// <summary> /// 灰度图像 HObject -> Bitmap /// </summary> public static Bitmap HObject2 ...