封装成类的 。 base64格式的字符串,只包含大小写字母、零到九,以及 + /

//___base_64.h

/*base_64.h文件*/
#ifndef BASE_64_H
#define BASE_64_H
/**
* Base64 编码/解码
* @author liruixing
*/
class Base64{
private:
std::string _base64_table;
static const char base64_pad = '=';public:
Base64()
{
_base64_table = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; /*这是Base64编码使用的标准字典*/
}
/**
* 这里必须是unsigned类型,否则编码中文的时候出错
*/
std::string Encode(const unsigned char * str,int bytes);
std::string Decode(const char *str,int bytes);
void Debug(bool open = true);
};
#endif
//___base_64.cpp
/*base_64.cpp文件*/
#include <iostream>
#include <string>
#include <cstring>
#include "base_64.h" std::string Base64::Encode(const unsigned char * str,int bytes) {
int num = ,bin = ,i;
std::string _encode_result;
const unsigned char * current;
current = str;
while(bytes > ) {
_encode_result += _base64_table[current[] >> ];
_encode_result += _base64_table[((current[] & 0x03) << ) + (current[] >> )];
_encode_result += _base64_table[((current[] & 0x0f) << ) + (current[] >> )];
_encode_result += _base64_table[current[] & 0x3f]; current += ;
bytes -= ;
}
if(bytes > )
{
_encode_result += _base64_table[current[] >> ];
if(bytes% == ) {
_encode_result += _base64_table[(current[] & 0x03) << ];
_encode_result += "==";
} else if(bytes% == ) {
_encode_result += _base64_table[((current[] & 0x03) << ) + (current[] >> )];
_encode_result += _base64_table[(current[] & 0x0f) << ];
_encode_result += "=";
}
}
return _encode_result;
}
std::string Base64::Decode(const char *str,int length) {
//解码表
const char DecodeTable[] =
{
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, , -, -, -, ,
, , , , , , , , , , -, -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -,
-, , , , , , , , , , , , , , , ,
, , , , , , , , , , , -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -,
-, -, -, -, -, -, -, -, -, -, -, -, -, -, -, -
};
int bin = ,i=,pos=;
std::string _decode_result;
const char *current = str;
char ch;
while( (ch = *current++) != '\0' && length-- > )
{
if (ch == base64_pad) { // 当前一个字符是“=”号
/*
先说明一个概念:在解码时,4个字符为一组进行一轮字符匹配。
两个条件:
1、如果某一轮匹配的第二个是“=”且第三个字符不是“=”,说明这个带解析字符串不合法,直接返回空
2、如果当前“=”不是第二个字符,且后面的字符只包含空白符,则说明这个这个条件合法,可以继续。
*/
if (*current != '=' && (i % ) == ) {
return NULL;
}
continue;
}
ch = DecodeTable[ch];
//这个很重要,用来过滤所有不合法的字符
if (ch < ) { /* a space or some other separator character, we simply skip over */
continue;
}
switch(i % )
{
case :
bin = ch << ;
break;
case :
bin |= ch >> ;
_decode_result += bin;
bin = ( ch & 0x0f ) << ;
break;
case :
bin |= ch >> ;
_decode_result += bin;
bin = ( ch & 0x03 ) << ;
break;
case :
bin |= ch;
_decode_result += bin;
break;
}
i++;
}
return _decode_result;
}

附1:C++实现 safaBase64格式跟nonSafeBase64格式的转换

附2:C++二进制字符串转十六进制字符串 十六进制字符串转二进制字符串

C++二进制字符串转Base64字符串 Base64字符串转二进制字符串的更多相关文章

  1. 字符串转base64,base64转字符串

    [JavaScript原生提供两个Base64相关方法] btoa():字符串或二进制值转为Base64编码 atob():Base64编码转为原来的编码 备注:利用这两个原生方法,我们来封装一下,标 ...

  2. 图片采用base64压缩,可以以字符串的形式传送base64给服务端转存为图片

    (function () { var coverImage = document.querySelector('<div id="coverImage">file< ...

  3. 设计DFA接受{0,1}上的字符串ω,且ω是3倍数的二进制表示

    DFA设计 设计DFA接受{0,1}上的字符串ω,且ω是3倍数的二进制表示 先叙述下思路: 要想证明某数是3的倍数可以让其除以3看余数是否为零即可,现在我们的问题就是如何计算一串二进制数除以3所得的余 ...

  4. 【编程题目】有 n 个长为 m+1 的字符串,如果某个字符串的最后 m 个字符与某个字符串的前 m 个字符匹配...

    37.(字符串)有 n 个长为 m+1 的字符串,如果某个字符串的最后 m 个字符与某个字符串的前 m 个字符匹配,则两个字符串可以联接,问这 n 个字符串最多可以连成一个多长的字符串,如果出现循环, ...

  5. JAVA----编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符

    package com.pb.demo.packclass.demo1; import java.util.HashSet; /** * 编程列出一个字符串的全字符组合情况,原始字符串中没有重复字符 ...

  6. json 字符串转换成对象,对象转换成json字符串

    json   字符串转换成对象,对象转换成json字符串 前端: 方法一: parseJSON方法:   [注意jquery版本问题] var str = '{"name":&qu ...

  7. 不区分大小写匹配字符串,并在不改变被匹配字符串的前提下添加html标签

    问题描述:最近在搭建一个开源平台网站,在做一个简单搜索的功能,需要将搜索到的结果中被匹配的字符串添加不一样的颜色,但是又不破坏被匹配的字符串. 使用的方法是替换被匹配的字符串加上font标签.但是搜索 ...

  8. 【C语言】字符串替换空格:实现一个函数,把字符串里的空格替换成“%20”

    //字符串替换空格:实现一个函数,把字符串里的空格替换成"%20" #include <stdio.h> #include <assert.h> void ...

  9. 4.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能: 1)输出字符串的长度。 2)输出字符串中第一个出现字母a的位置。 3)在字符串的第3个字符后面插入子串“hello”,输出新字符串。 4)将字符串“hello”替换为“me”,输出新字符串。 5)以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。 */

    namespace test4 {/* 4.写一个控制台应用程序,接收一个长度大于3的字符串,完成下列功能: 1)输出字符串的长度. 2)输出字符串中第一个出现字母a的位置. 3)在字符串的第3个字符 ...

  10. C 语言 字符串命令 strstr()的用法 实现将原字符串以分割串分割输出

    C 语言 字符串命令 strstr()的用法 实现将原字符串以分割串分割输出 strstr() 命令是在原字符串中查找指定的字符串第一次出现的地址,用这个特性可以实现字符的分割,判断是否包涵等功能: ...

随机推荐

  1. [No000013B]初识Ildasm.exe——IL反编译的实用工具

    Ildasm.exe 概要: 一.前言: 微软的IL反编译实用程序——Ildasm.exe,可以对可执行文件(ex,经典的控制台Hello World 的 exe 可执行文件)抽取出 IL 代码,并且 ...

  2. [No0000D4]批处理全部代码详解Allbat

    COPY REM Copies one or more files from one location to another. REM [/d] - Allows the encrypted file ...

  3. 知识驱动对话-Learning to Select Knowledge for Response Generation in Dialog Systems-阅读笔记

    今日看了一篇文章<Learning to Select Knowledge for Response Generation in Dialog Systems>,以知识信息.对话目标.对话 ...

  4. 1.7Oob 成员变量可以不初始化 但局部变量必须初始化

    成员变量有默认的初始值 像int a; a默认为0:而局部变量没有初始值

  5. git修改提交的用户名

    场景分析 试想一下这样的场景,你先在自己的电脑上上创建了一个项目,并且把他添加到了git仓库,然后提交到了Github.但是,下一次,你在公司的电脑上clone了这个项目,并且修改了文件,当你提交到G ...

  6. C和C指针小记(四)-浮点类型

    1.浮点型 浮点数家族包括:float,double,long double. ASCII标准规定:long double 至少和 double 一样长,而 double 至少和float 一样长.同 ...

  7. [daily][tcpdump] tcpdump查找reset包

    tcpdump -r xxxx.pcap 'tcp[tcpflags] & tcp-rst != 0'

  8. 适配ipad Pro

    买了个新款ipad pro 11寸的,发现我们的ipad项目. 上下左右都有黑边 使用info.plist设置启动图,不用asset和launch storyboard 图片用如下格式命名,拖到项目中 ...

  9. 转:ActiveMQ的作用总结(应用场景及优势)

    原文地址: ActiveMQ的作用总结(应用场景及优势) 业务场景说明: 消息队列在大型电子商务类网站,如京东.淘宝.去哪儿等网站有着深入的应用, 队列的主要作用是消除高并发访问高峰,加快网站的响应速 ...

  10. jquery-- json字符串没有自动包装为 json对象

    jquery 的一个坑 页面使用 ajax ,回调函数中获取后端返回的 json 格式数据(ajax 未显式指定返回值类型),后端controller方法标注 @ResponseBody 并返回一个 ...