与Web交互可用的图片Base64编码
#ifndef ___BASE64_H___
#define ___BASE64_H___ #include <string> using namespace std; class CBase64
{
public:
CBase64();
~CBase64(); /*********************************************************
* 函数说明:将输入数据进行base64编码
* 参数说明:[in]pIn 需要进行编码的数据
[in]uInLen 输入参数的字节数
[out]strOut 输出的进行base64编码之后的字符串
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Encode(const unsigned char *pIn, unsigned long uInLen, string& strOut); /*********************************************************
* 函数说明:将输入数据进行base64编码
* 参数说明:[in]pIn 需要进行编码的数据
[in]uInLen 输入参数的字节数
[out]pOut 输出的进行base64编码之后的字符串
[out]uOutLen 输出的进行base64编码之后的字符串长度
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Encode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen); /*********************************************************
* 函数说明:将输入数据进行base64解码
* 参数说明:[in]strIn 需要进行解码的数据
[out]pOut 输出解码之后的节数数据
[out]uOutLen 输出的解码之后的字节数长度
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Decode(const string& strIn, unsigned char *pOut, unsigned long *uOutLen) ; /*********************************************************
* 函数说明:将输入数据进行base64解码
* 参数说明:[in]strIn 需要进行解码的数据
[out]pOut 输出解码之后的节数数据
[out]uOutLen 输出的解码之后的字节数长度
* 返回值 :true处理成功,false失败
**********************************************************/
bool static Decode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen) ;
}; #endif // ___BASE64_H___
#include "stdafx.h" /*
*
*Base64?
*
*
*
*/ #include "Base64.h" static const char *g_pCodes =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/="; static const unsigned char g_pMap[] =
{
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , , , , , , , , , , ,
, , ,
}; CBase64::CBase64()
{
} CBase64::~CBase64()
{
} bool CBase64::Encode(const unsigned char *pIn, unsigned long uInLen, unsigned char *pOut, unsigned long *uOutLen)
{
unsigned long i, len2, leven;
unsigned char *p; if(pOut == NULL || *uOutLen == )
return false; //ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL)); len2 = ((uInLen + ) / ) << ;
if((*uOutLen) < (len2 + )) return false; p = pOut;
leven = * (uInLen / );
for(i = ; i < leven; i += )
{
*p++ = g_pCodes[pIn[] >> ];
*p++ = g_pCodes[((pIn[] & ) << ) + (pIn[] >> )];
*p++ = g_pCodes[((pIn[] & 0xf) << ) + (pIn[] >> )];
*p++ = g_pCodes[pIn[] & 0x3f];
pIn += ;
} if (i < uInLen)
{
unsigned char a = pIn[];
unsigned char b = ((i + ) < uInLen) ? pIn[] : ;
unsigned char c = ; *p++ = g_pCodes[a >> ];
*p++ = g_pCodes[((a & ) << ) + (b >> )];
*p++ = ((i + ) < uInLen) ? g_pCodes[((b & 0xf) << ) + (c >> )] : '=';
*p++ = '=';
} *p = ; // Append NULL byte
*uOutLen = p - pOut;
return true;
} bool CBase64::Encode(const unsigned char *pIn, unsigned long uInLen, string& strOut)
{
unsigned long i, len2, leven;
strOut = ""; //ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL)); len2 = ((uInLen + ) / ) << ;
//if((*uOutLen) < (len2 + 1)) return false; //p = pOut;
leven = * (uInLen / );
for(i = ; i < leven; i += )
{
strOut += g_pCodes[pIn[] >> ];
strOut += g_pCodes[((pIn[] & ) << ) + (pIn[] >> )];
strOut += g_pCodes[((pIn[] & 0xf) << ) + (pIn[] >> )];
strOut += g_pCodes[pIn[] & 0x3f];
pIn += ;
} if (i < uInLen)
{
unsigned char a = pIn[];
unsigned char b = ((i + ) < uInLen) ? pIn[] : ;
unsigned char c = ; strOut += g_pCodes[a >> ];
strOut += g_pCodes[((a & ) << ) + (b >> )];
strOut += ((i + ) < uInLen) ? g_pCodes[((b & 0xf) << ) + (c >> )] : '=';
strOut += '=';
} //*p = 0; // Append NULL byte
//*uOutLen = p - pOut;
return true;
} bool CBase64::Decode(const string& strIn, unsigned char *pOut, unsigned long *uOutLen)
{
unsigned long t, x, y, z;
unsigned char c;
unsigned long g = ; //ASSERT((pIn != NULL) && (uInLen != 0) && (pOut != NULL) && (uOutLen != NULL)); for(x = y = z = t = ; x < strIn.length(); x++)
{
c = g_pMap[strIn[x]];
if(c == ) continue;
if(c == ) { c = ; g--; } t = (t << ) | c; if(++y == )
{
if((z + g) > *uOutLen) { return false; } // Buffer overflow
pOut[z++] = (unsigned char)((t>>)&);
if(g > ) pOut[z++] = (unsigned char)((t>>)&);
if(g > ) pOut[z++] = (unsigned char)(t&);
y = t = ;
}
} *uOutLen = z;
return true;
}
与Web交互可用的图片Base64编码的更多相关文章
- 对JSON传递图片Base64编码的一点总结
项目中跟Java对接的时候需要传输图片,经过Base64编码后传输的. 但是实际调试的时候发现Java那边始终无法正常解析出图片. 冷静想想之后,发现问题在于使用OpenCV读取图片,编码的是Mat: ...
- [转]玩转图片Base64编码
转自:[前端攻略]:玩转图片Base64编码 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的 base64 编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的 base64 ...
- 在线图片base64编码
图片Base64编码https://oktools.net/image2base64 在线工具https://oktools.net JSON格式化https://oktools.net/json U ...
- 图片base64编码解码
1.图片base64编码 https://c.runoob.com/front-end/59 2.图片base64解码 https://www.it399.com/image/base64 https ...
- 【前端攻略】:玩转图片Base64编码
引言 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的 base64 编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的 base64 编码.标题略大,不过只是希望通过一些浅显 ...
- 玩转图片Base64编码
什么是 base64 编码? 图片的 base64 编码就是可以将一副图片数据编码成一串字符串,使用该字符串代替图像地址. 这样做有什么意义呢?我们知道,我们所看到的网页上的每一个图片,都是需要消耗一 ...
- 【前端攻略】:玩转图片Base64编码(转)
引言 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的Base64编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的base64编码.标题略大,不过只是希望通过一些浅显的论述, ...
- 《转》玩转图片Base64编码
引言 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的 base64 编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的 base64 编码.标题略大,不过只是希望通过一些浅显 ...
- 图片Base64编码
我们经常在做Jquery插件的时候需要插入一些自定义的控件,比如说按钮,而我们自己又觉着button标签很丑,而且不同浏览器显示的效果还不一样,这个时候我们需要用到图片,当然,我们可以通过img标签添 ...
随机推荐
- [随时更新] Git的过滤规则 .gitignore 配置
往github上传代码的时候,很多文件没必要都传,这就需要在.gitignore文件里配置一下过滤规则.在此记录一下各种项目的配置参数,先从最近做的android开始. 原文地址请保留http://w ...
- .NET(C#)如何遍历Dictionary
我们知道.NET中的Dictionary是键/值对的集合,使用起来也是比较方便,Dictionary也可以用KeyValuePair来迭代遍历,具体如下: using System; using Sy ...
- 【python】python之tuple元组
tuple特性 python的tuple与列表类似,不同之处在于tuple的元素不能修改. tuple使用小括号,列表使用方括号. tuple创建很简单,只需要在括号中添加元素,并使用逗号隔开即可. ...
- 使用 Azure PowerShell 模块创建和管理 Windows VM
Azure 虚拟机提供完全可配置的灵活计算环境. 本教程介绍 Azure 虚拟机的基本部署项目,例如选择 VM 大小.选择 VM 映像和部署 VM. 你将学习如何执行以下操作: 创建并连接到 VM 选 ...
- 初识java内存区域
目录: 1.运行时数据区域 2.对象的创建 3.对象的内存布局 4.对象的访问定位 一.运行时数据区域 基本的java虚拟机运行时数据区如下图: 下面我们就来逐个认识这几个运行时的数据区域 1.程序计 ...
- windows环境下 nginx+iis 反向代理解决跨域问题
项目基本完成,是时候花点时间整理一下最近的姿势了 1 什么是跨域? 网上对于跨域的概念会有大篇幅的文章去解释,似乎有点玄乎,初学者很容易对这个概念产生恐惧,跨域其实很简单,其实只要知道一点,无法跨域访 ...
- pycharm的放大和缩小字体的显示 和ubunt的截圖工具使用 ubuntu上安装qq微信等工具
https://www.cnblogs.com/sui776265233/p/9322074.html#_label0 ubuntu: 截圖工具的使用 在ubuntu 10.04 的时候,还可以很方便 ...
- DELL MD3200i存储控制器解锁方法
DELL MD3200i存储控制器解锁方法 现有一台DELL MD3200i存储,因种种原因导致控制器被锁定,这里是刚出厂的一台存储,出现这个问题让我们都很困惑,只能怀疑DELL公司的问题. 这台存储 ...
- php框架安装
安装yii框架 跳转到composer.phar目录 cd C:\ProgramData\ComposerSetup\bin 安装yii2高级版 php composer.phar create-pr ...
- SDN2017 第二次实验作业
安装floodlight 参考链接:http://www.sdnlab.com/19189.html 从github下载源码,并编译安装 $ sudo apt-get install build-es ...