#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编码的更多相关文章

  1. 对JSON传递图片Base64编码的一点总结

    项目中跟Java对接的时候需要传输图片,经过Base64编码后传输的. 但是实际调试的时候发现Java那边始终无法正常解析出图片. 冷静想想之后,发现问题在于使用OpenCV读取图片,编码的是Mat: ...

  2. [转]玩转图片Base64编码

    转自:[前端攻略]:玩转图片Base64编码 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的 base64 编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的 base64 ...

  3. 在线图片base64编码

    图片Base64编码https://oktools.net/image2base64 在线工具https://oktools.net JSON格式化https://oktools.net/json U ...

  4. 图片base64编码解码

    1.图片base64编码 https://c.runoob.com/front-end/59 2.图片base64解码 https://www.it399.com/image/base64 https ...

  5. 【前端攻略】:玩转图片Base64编码

    引言 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的 base64 编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的 base64 编码.标题略大,不过只是希望通过一些浅显 ...

  6. 玩转图片Base64编码

    什么是 base64 编码? 图片的 base64 编码就是可以将一副图片数据编码成一串字符串,使用该字符串代替图像地址. 这样做有什么意义呢?我们知道,我们所看到的网页上的每一个图片,都是需要消耗一 ...

  7. 【前端攻略】:玩转图片Base64编码(转)

    引言 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的Base64编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的base64编码.标题略大,不过只是希望通过一些浅显的论述, ...

  8. 《转》玩转图片Base64编码

    引言 图片处理在前端工作中可谓占据了很重要的一壁江山.而图片的 base64 编码可能相对一些人而言比较陌生,本文不是从纯技术的角度去讨论图片的 base64 编码.标题略大,不过只是希望通过一些浅显 ...

  9. 图片Base64编码

    我们经常在做Jquery插件的时候需要插入一些自定义的控件,比如说按钮,而我们自己又觉着button标签很丑,而且不同浏览器显示的效果还不一样,这个时候我们需要用到图片,当然,我们可以通过img标签添 ...

随机推荐

  1. LeetCode题解之 Merge k Sorted Lists

    1.题目描述 2.问题分析 使用合并两个链表的方法,逐次合并,效率较低.可以考虑同时合并K个链表. 3.代码 ListNode* mergeKLists(vector<ListNode*> ...

  2. linux下搭建hexo环境

    最近对搭建个人博客比较感兴趣,但是刚搭建好next主题基本博客,电脑就坏了,借了一台电脑继续搞,不想在他电脑中弄太多环境,所以我准备在自己电脑的服务器上搭建hexo环境 服务器环境: (1)cento ...

  3. Prometheus Node_exporter 之 Memory Detail Meminfo /proc/meminfo

    1. Memory Active / Inactive type: GraphUnit: bytesLabel: BytesInactive - 最近使用较少的内存, 优先被回收利用 /proc/me ...

  4. Oracle EBS CST 成本请求报错

    (文档 ID 430533.1) When running CMCPAW, Periodic Actual Cost Worker,  an error is received in the logf ...

  5. python自学——文件修改

    #如何修改文件,我们知道文件因为在磁盘上已经有储存了,后面要更新或修改,只能在在原来文件后面追加使用f=open("wenjian_name","r+",enc ...

  6. Linux运维之--zabbix使用(实时更新)

    之前安装的是zabbix3.x版本,今天尝试安装zabbix4.2版本,并做个总结.建议生产环境还是使用3.4版本比较好,因为4.2版本上可能语法又增加了一些,所以建议使用熟练的版本 1.首先是安装z ...

  7. Tidb数据库报错:Transaction too large

    Tidb是一个支持ACID的分布式数据库,当你导入一个非常大的数据集时,这时候产生的事务相当严重,并且Tidb本身对事物的大小也是有一个严格的控制. 有事务大小的限制主要在于 TiKV 的实现用了一致 ...

  8. ISO8583组包、解包

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace POS. ...

  9. 关于Excel中的行列转换

    1. 先选择想要的数据进行复制 然后选择你要粘贴的位置点击 “选择性粘贴” 点击  “转置” 完成

  10. beta冲刺————第五天(5/5=1)

    今天的主要内容是前后端的对接: 通过前几天的对接,我们发现后端传给前端内容是可以很完美的显示出来的,说明文章格式以及一些默认规则都是OK的. 然后就是前端从云服务器上面接受到文章的具体内容,在这一个环 ...