C++ 把字符串转为base64

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了C++ 把字符串转为base64相关的知识,希望对你有一定的参考价值。

我要把"abc123" 转为 base64的格式 怎么转

参考技术A 在 ATLENC.H 里有一个库函数:
BOOL Base64Encode(
_In_reads_(nSrcLen) const BYTE *pbSrcData,
_In_ int nSrcLen,
_When_(*pnDestLen > 0, _Out_writes_to_(*pnDestLen, *pnDestLen)) LPSTR szDest,
_Inout_ int *pnDestLen,
_In_ DWORD dwFlags = ATL_BASE64_FLAG_NONE)

用法:

input: buf(byte), buflen, out StringA, outLen, 转换模式.
Base64Encode(buf,buflen,strA.GetBuffer(outlen),&outlen, ATL_BASE64_FLAG_NONE))追问

没看懂啊, 我不怎么会.
CString cs1 = _T("abc123456");
转码后应该是byte byte1[] 这个类型吧.
用他接收,怎么写啊

追答

0.0

Base64Encode 的输入是 byte*, 输出是LPSTR szDest


给你贴一段代码:

CString strSSL = _T("abc123456");
CStringA strTemp = CT2A(strSSL);
unsigned char* pBuf = (unsigned char*)strTemp.GetBuffer(strTemp.GetLength());
CStringA strA;
int bufLen = strTemp.GetLength()'
int outlen = bufLen *2;
if(EncodeBase64(pBuf,bufLen ,strA.GetBuffer(outlen),&outlen, ATL_BASE64_FLAG_NONE))

strA.ReleaseBuffer(outlen);
outString = CA2T(strA);

追问

谢谢啊,不过怎么放里面报错啊.

追答

=.= 你要include 对应的头文件啊...
ATLENC.H

本回答被提问者和网友采纳
参考技术B #define BASE64_CHARS "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"
//---------------------------------------------------------------------------
int mxcBase64Encode(const unsigned char * str,int len,unsigned char* &rt)

unsigned char c,c1,c2,c3;
char a[]=BASE64_CHARS;
int i=0;
int r=0;
if(rt)free(rt),rt=0;;
while(1)

if(i<len)c1=str[i++];elsei=0; break;
if(i<len)c2=str[i++];elsei=1; break;
if(i<len)c3=str[i++];elsei=2; break;
c=c1>>2;
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=a[c];
c=c1<<6;
c=(c>>2) | (c2>>4);
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=a[c];
c=c2<<4;
c=(c>>2) | (c3>>6);
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=a[c];
c=c3&0x3F;
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=a[c];

if(i==1)

c=c1>>2;
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=a[c];
c=c1<<6;
c=(c>>2);
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=a[c];
rt=(unsigned char*)realloc(rt,r+2);
rt[r++]='=';
rt[r++]='=';

else if(i==2)

c=c1>>2;
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=a[c];
c=c1<<6;
c=(c>>2) | (c2>>4);
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=a[c];
c=c2<<4;
c=(c>>2);
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=a[c];
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]='=';

return r;

//---------------------------------------------------------------------------
int mxcBase64Decode(char *src,unsigned char* &rt)

unsigned char c,c1,c2,c3,c4;
char a[]=BASE64_CHARS;
int i=0;
if(rt)free(rt),rt=0;;
int r=0;
char *p=src,*p1=NULL;
while(*p)

if(*p&&*p!='=')p1=strchr(a,*p++);if(p1)c1=p1-a;else break;elsei=0; break;
if(*p&&*p!='=')p1=strchr(a,*p++);if(p1)c2=p1-a;else break;elsei=1; break;
if(*p&&*p!='=')p1=strchr(a,*p++);if(p1)c3=p1-a;else break;elsei=2; break;
if(*p&&*p!='=')p1=strchr(a,*p++);if(p1)c4=p1-a;else break;elsei=3; break;
c=(c1<<2) | (c2>>4);
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=c;
c=(c2<<4) | (c3>>2);
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=c;
c=(c3<<6) | c4;
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=c;

if(p1)

if(i==2)

c=(c1<<2) | (c2>>4);
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=c;

else if(i==3)

c=(c1<<2) | (c2>>4);
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=c;
c=(c2<<4) | (c3>>2);
rt=(unsigned char*)realloc(rt,r+1);
rt[r++]=c;


return r;

//---------------------------------------------------------------------------
参考技术C h
hhhhhhhhhhhhhhhhhhh
参考技术D C++base64字符串转换

1、下面是头文件定义:

#ifndef _BASE64_H_
#define _BASE64_H_

#include<string>
using namespace std;

//#include <conf.h>
class CBase64Coder

private :
static char ch64[];
char* buf;
int size ;

private :
static int BinSearch(char p);
void allocMem(int NewSize);

public :
CBase64Coder();

2、下面是头文件定义:

#ifndef _BASE64_H_
#define _BASE64_H_

#include<string>
using namespace std;

//#include <conf.h>
class CBase64Coder

private :
static char ch64[];
char* buf;
int size ;

private :
static int BinSearch(char p);
void allocMem(int NewSize);

public :
CBase64Coder();
~CBase64Coder();
const char* encode(const string& message);
const char* encode(const char* buffer,int buflen);
const char* decode(const char* buffer,int Length);

;

#endif

3、下面是类方法实现:

#include "CBase64Coder.h"

char CBase64Coder::ch64[] =
'A','B','C','D','E','F','G','H','I','J','K','L','M','N',
'O','P','Q','R','S','T','U','V','W','X','Y','Z',
'a','b','c','d','e','f','g','h','i','j','k','l','m','n',
'o','p','q','r','s','t','u','v','w','x','y','z',
'0','1','2','3','4','5','6','7','8','9','+','/','='
;

CBase64Coder::CBase64Coder()

buf = NULL;
size = 0 ;


CBase64Coder::~CBase64Coder()

if ( buf )
free(buf);


void CBase64Coder::allocMem(int NewSize)

if ( buf )
buf = (char*)realloc(buf,NewSize);
else
buf = (char*)malloc(NewSize);
memset(buf,0,NewSize);


const char* CBase64Coder::encode(const string& buffer)

return encode(buffer.c_str(),buffer.length());


const char* CBase64Coder::encode(const char* buffer,int buflen)

int nLeft = 3 - buflen%3 ;
//根据BASE64算法,总长度会变成原来的4/3倍
//所以内存分配=length*4/3并加1位作为结束符号(0)
allocMem(( buflen + nLeft )*4/3+1);
//临时变量,
char ch[4];
int i ,j;
for ( i = 0 ,j = 0; i < ( buflen - buflen%3 ); i += 3,j+= 4 )

ch[0] = (char)((buffer[i]&0xFC) >> 2 );
ch[1] = (char)((buffer[i]&0x03) << 4 | (buffer[i+1]&0xF0) >> 4 );
ch[2] = (char)((buffer[i+1]&0x0F) << 2 | (buffer[i+2]&0xC0) >> 6 );
ch[3] = (char)((buffer[i+2]&0x3F));
//查询编码数组获取编码后的字符
buf[j] = ch64[ch[0]];
buf[j+1] = ch64[ch[1]];
buf[j+2] = ch64[ch[2]];
buf[j+3] = ch64[ch[3]];


if ( nLeft == 2 )

ch[0] = (char)((buffer[i]&0xFC) >> 2);
ch[1] = (char)((buffer[i]&0x3) << 4 );
ch[2] = 64;
ch[3] = 64;

//查询编码数组获取编码后的字符
buf[j] = ch64[ch[0]];
buf[j+1] = ch64[ch[1]];
buf[j+2] = ch64[ch[2]];
buf[j+3] = ch64[ch[3]];

else if ( nLeft == 1 )

ch[0] = (char)((buffer[i]&0xFC) >> 2 );
ch[1] = (char)((buffer[i]&0x03) << 4 | (buffer[i+1]&0xF0) >> 4 );
ch[2] = (char)((buffer[i+1]&0x0F) << 2 );
ch[3] = 64;

//查询编码数组获取编码后的字符
buf[j] = ch64[ch[0]];
buf[j+1] = ch64[ch[1]];
buf[j+2] = ch64[ch[2]];
buf[j+3] = ch64[ch[3]];

return buf;


const char* CBase64Coder::decode(const char* buffer,int Length)

int length = Length;
if ( length%4 != 0 )
return NULL;

allocMem(length*3/4 + 1);

char p;
char ch[4];
int i , j ;
for ( i = 0 , j = 0 ; i < length ; i += 4 , j+= 3)

for ( int z = 0 ; z < 4 ; z++)

//采用2分法查找
p = (char)BinSearch(buffer[i+z]);
if ( p == -1 )
return NULL;
ch[z] = p;


buf[j] = (char)((ch[0]&0x3F) << 2 | (ch[1]&0x30) >> 4 );
buf[j+1] = (char)((ch[1]&0xF) <<4 | (ch[2]&0x3C) >>2 );
buf[j+2] = (char)((ch[2]&0x03) << 6 | (ch[3]&0x3F));

return buf;


//采用二分法查找p在ch64数组中的位置,并返回。如果找不到则返回-1
int CBase64Coder::BinSearch(char p)

if ( p >= 'A' && p <= 'Z' )
return (p - 'A');
else if ( p >= 'a' && p <= 'z' )
return (p - 'a' + 26);
else if ( p >= '0' && p <= '9' )
return (p - '0' + 26 + 26);
else if ( p == '+' )
return 62;
else if ( p == '/' )
return 63;
else if ( p == '=' )
return 64;
return -1;

如何将音频文件转为base64 编码

参考技术A 网上有一些在线的转换的工具的,将图片或者音频转换为base64b编码,可以直接放到css中使用的

以上是关于C++ 把字符串转为base64的主要内容,如果未能解决你的问题,请参考以下文章

如何将音频文件转为base64 编码

JAVA将图片(本地或者网络资源)转为Base64字符串,将base64字符串存储为本地图片

ios将base64字符串转为图片,点击图片全屏展示

Base64简单原理

js base64的转码与解码

nodejs对字符串进行base64转换和解析