C++实现的base64字节大文件文件编码并保存到文本

源码及DLL下载

20210827补充源码上传(蓝凑云)

下载:https://pricexiao.lanzoui.com/in6hIt82jsb 密码:2wrt

C++BASE64源码

lib.h文件(弃用,更新的代码放在后面了)



#include "stdafx.h"
#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <cstring>
bool is_readable(const std::string & file)
{
	wchar_t szBuffer[1024*10];
	OemToCharW(file.c_str(), szBuffer);
	std::ifstream fichier(szBuffer);
	return !fichier.fail();
}

// Base 64

static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";
static const int bufferSize = 1024 * 10;

static inline bool is_base64(unsigned char c) {
	return (isalnum(c) || (c == '+') || (c == '/'));
}

std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len)
{
	std::string ret;
	int i = 0;
	int j = 0;
	unsigned char char_array_3[3];
	unsigned char char_array_4[4];

	while (in_len--) {
		char_array_3[i++] = *(bytes_to_encode++);
		if (i == 3) {
			char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
			char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
			char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
			char_array_4[3] = char_array_3[2] & 0x3f;

			for (i = 0; (i < 4); i++)
				ret += base64_chars[char_array_4[i]];
			i = 0;
		}
	}

	if (i)
	{
		for (j = i; j < 3; j++)
			char_array_3[j] = '\0';

		char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
		char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
		char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);

		for (j = 0; (j < i + 1); j++)
			ret += base64_chars[char_array_4[j]];

		while ((i++ < 3))
			ret += '=';

	}

	return ret;

}

std::string hex_string(std::string hexstr)
{
	std::string str = "";
	str.resize((hexstr.size() + 1) / 2);
	for (size_t i = 0, j = 0; i < str.size(); i++, j++)
	{
		char at = '@';
		str[i] = (hexstr[j] & at ? hexstr[j] + 9 : hexstr[j]) << 4, j++;
		str[i] |= (hexstr[j] & at ? hexstr[j] + 9 : hexstr[j]) & 0xF;
	}
	return str;
}

std::string string_hex(std::string str, const bool capital = false)
{
	std::string hexstr = "";
	hexstr.resize(str.size() * 2);
	static const char a = capital ? 0x40 : 0x60;
	for (size_t i = 0; i < str.size(); i++)
	{
		char c = (str[i] >> 4) & 0xF;
		hexstr[i * 2] = c > 9 ? (c - 9) | a : c | '0';
		hexstr[i * 2 + 1] = (str[i] & 0xF) > 9 ? (str[i] - 9) & 0xF | a : str[i] & 0xF | '0';
	}
	return hexstr;
}

std::string char_to_string(char x[], int size_recv)
{
	int num_car = 0;
	int stop_while = size_recv;
	std::string output = "";

	while (stop_while > 0)
	{
		output += x[num_car];
		if (num_car < size_recv) { num_car++; }

		stop_while--;
	}

	return output;
}

std::string str_base64_encode(std::string str)
{
	return base64_encode(reinterpret_cast<const unsigned char*>(str.c_str()), str.length());
}

std::string base64_decode(std::string const& encoded_string) {
	int in_len = encoded_string.size();
	int i = 0;
	int j = 0;
	int in_ = 0;
	unsigned char char_array_4[4], char_array_3[3];
	std::string ret;

	while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
		char_array_4[i++] = encoded_string[in_]; in_++;
		if (i == 4) {
			for (i = 0; i < 4; i++)
				char_array_4[i] = base64_chars.find(char_array_4[i]);

			char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
			char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
			char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

			for (i = 0; (i < 3); i++)
				ret += char_array_3[i];
			i = 0;
		}
	}

	if (i) {
		for (j = 0; j < i; j++)
			char_array_4[j] = base64_chars.find(char_array_4[j]);

		char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
		char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);

		for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
	}

	return ret;
}

// Decode file

int decode_file_base64(std::string path_in, std::string path_out)
{
	std::string path_file = path_in;

	if (is_readable(path_file))
	{
		FILE *file_input;
		FILE *file_output;
		fopen_s(&file_input, path_file.c_str(), "ab+");
		fopen_s(&file_output, path_out.c_str(), "ab+");

		char buff_read[bufferSize];

		if (file_input != NULL)
		{

			std::string str_encode_buff = "";
			bool lb_read = true;
			while (lb_read)
			{
				lb_read = fread(buff_read, sizeof(buff_read), 1, file_input);
				str_encode_buff += char_to_string(buff_read, sizeof(buff_read));
				memset(buff_read, 0, sizeof(buff_read));
			}
			str_encode_buff = base64_decode(str_encode_buff);

			fwrite(str_encode_buff.c_str(), strlen(string_hex(str_encode_buff).c_str()) / 2, 1, file_output);

			fclose(file_input);
			fclose(file_output);
		}
		else{
			return -1;
		}
	}
	else{
		return -2;
	}
	return 0;
}

int decode_string_base64_file(std::string path_in, std::string path_out)
{
	std::string path_file = path_in;

	//if (is_readable(path_file))
	//{
		//FILE *file_input;
		FILE *file_output;
		//fopen_s(&file_input, path_file.c_str(), "ab+");
		fopen_s(&file_output, path_out.c_str(), "ab+");

		//char buff_read[1];

		if (!path_file.empty())
		{
			//std::string str_encode_buff = "";
			//while (fread(buff_read, sizeof(buff_read), 1, file_input))
			//{
				//str_encode_buff += char_to_string(buff_read, sizeof(buff_read));
				//memset(buff_read, 0, sizeof(buff_read));
			//}
			path_file = base64_decode(path_file);

			fwrite(path_file.c_str(), strlen(string_hex(path_file).c_str()) / 2, 1, file_output);

			//fclose(file_input);
			fclose(file_output);
		}
		else{
			return -1;
		}
	//}
	return 0;
}

int encode_file_base64(std::string path_in, std::string path_out)
{
	std::string path_file = path_in;

	if (is_readable(path_file))
	{
		FILE *file_input;
		FILE *file_output;

		fopen_s(&file_input, path_file.c_str(), "ab+");
		fopen_s(&file_output, path_out.c_str(), "ab+");

		char buff_read[bufferSize];

		if (file_input != NULL)
		{
			std::string str_encode_buff = "";
			bool lb_read = true;
			while (lb_read)
			{
				lb_read = fread(buff_read, sizeof(buff_read), 1, file_input);
				str_encode_buff += char_to_string(buff_read, sizeof(buff_read));
				memset(buff_read, 0, sizeof(buff_read));
			}

			str_encode_buff = str_base64_encode(str_encode_buff);
			fwrite(str_encode_buff.c_str(), strlen(string_hex(str_encode_buff).c_str()) / 2, 1, file_output);

			fclose(file_input);
			fclose(file_output);
		}
		else{
			return -1;
		}
	}
	else{
		return -2;
	}

	return 0;
}

int encode_file_base64_string(std::string path_in, OUT char* content)
{
	std::string path_file = path_in;

	if (is_readable(path_file))
	{
		FILE *file_input;
		//FILE *file_output;

		fopen_s(&file_input, path_file.c_str(), "ab+");
		//fopen_s(&file_output, path_out.c_str(), "ab+");

		char buff_read[bufferSize];

		if (file_input != NULL)
		{
			std::string str_encode_buff = "";
			bool lb_read = true;
			while (lb_read)
			{
				lb_read = fread(buff_read, sizeof(buff_read), 1, file_input);
				str_encode_buff += char_to_string(buff_read, sizeof(buff_read));
				memset(buff_read, 0, sizeof(buff_read));
			}

			str_encode_buff = str_base64_encode(str_encode_buff);
			//fwrite(str_encode_buff.c_str(), strlen(string_hex(str_encode_buff).c_str()) / 2, 1, file_output);

			fclose(file_input);
			//fclose(file_output);
			strcpy_s(content, strlen(str_encode_buff.c_str()) + 1, str_encode_buff.c_str());
			//strcpy(content, str_encode_buff.c_str);
			//content = str_encode_buff;
		}
		else{
			return -1;
		}
	}
	else{
		return -2;
	}

	return 0;
}

std::string encode_file_base64_string(std::string path_in)
{
	std::string path_file = path_in;
	std::string str_encode_buff = "";
	if (is_readable(path_file))
	{
		FILE *file_input;
		//FILE *file_output;

		fopen_s(&file_input, path_file.c_str(), "ab+");
		//fopen_s(&file_output, path_out.c_str(), "ab+");

		char buff_read[bufferSize];

		if (file_input != NULL)
		{
			bool lb_read = true;
			while (lb_read)
			{
				lb_read = fread(buff_read, sizeof(buff_read), 1, file_input);
				str_encode_buff += char_to_string(buff_read, sizeof(buff_read));
				memset(buff_read, 0, sizeof(buff_read));
			}

			str_encode_buff = str_base64_encode(str_encode_buff);
			//fwrite(str_encode_buff.c_str(), strlen(string_hex(str_encode_buff).c_str()) / 2, 1, file_output);

			fclose(file_input);
			//fclose(file_output);
			str_encode_buff;
		}
		else{
			str_encode_buff = "-1";
		}
	}
	else{
		str_encode_buff = "-2";
	}

	return str_encode_buff;
}


pbBase64Func.cpp文件

// pbBase64Func.cpp : 定义 DLL 应用程序的导出函数。
//

#include "stdafx.h"
#include "pbBase64Func.h"

int _stdcall filetoBase64(char* filePath, char* outPut){
	return encode_file_base64(filePath, outPut);
}
int _stdcall base64ToFile(char* filePath, char* outPut){
	return decode_file_base64(filePath, outPut);
}
int _stdcall filetoBase64ToString(char* filePath, OUT char* content){
	return encode_file_base64_string(filePath, OUT content);
}
int _stdcall base64ContentToFile(char* content, char* outPut){
	return decode_string_base64_file(content, outPut);
}
/*char* filetoBase64ToString2(char* filePath){
	std::string str = encode_file_base64_string(filePath);
	char * strc = new char[strlen(str.c_str()) + 1];
	
	return strc;
}*/

pbBase64Func.h文件

#include "stdafx.h"
#include "lib.h"
#include <iostream>
#include <string>

extern "C" _declspec(dllexport) int _stdcall filetoBase64(char* filePath, char* outPut);
extern "C" _declspec(dllexport) int _stdcall base64ToFile(char* filePath, char* outPut);
extern "C" _declspec(dllexport) int _stdcall filetoBase64ToString(char* filePath, OUT char* content);
extern "C" _declspec(dllexport) int _stdcall base64ContentToFile(char* content, char* outPut);
//extern "C" _declspec(dllexport) char* _stdcall filetoBase64ToString2(char* filePath);

pbBase64Func.def文件

LIBRARY

EXPORTS
	filetoBase64 @ 1
	base64ToFile @ 2
	filetoBase64ToString @ 3
	base64ContentToFile @ 4

PB使用范例

//-----
String ls_pic1name,export_filename
If Getfilesavename("选择保存路径",ls_pic1name,export_filename,&
"zip","zip Files (*.zip),*.zip,"+ "jpg Files (*.jpg),*.jpg"+",bmp Files (*.bmp),*.bmp") <> 1 Then 
	is_filename = ''
	return
Else
	tab_1.tabpage_4.sle_4.text = ls_pic1name
	is_filename = export_filename
End If


//-------
ls_filepath = sle_4.text
If FileExists(ls_filepath) Then
Else
	messageBox('提示','你选择的文件不存在,请重新选择!')
	return 
End If
If isNUll(is_filename) or is_filename='' Then
	messageBox('提示','请选择文件!')
	return
End If

///------
Ls_outputpath = gs_rundir+'GXNNYB_BASE64TXT\'
If Not DirectoryExists ( Ls_outputpath ) Then
	CreateDirectory ( Ls_outputpath )
End If
Ls_outputpath = Ls_outputpath +String(Now(),'YYYYMMDD') + '_BASE64_'+is_filename+'.TXT'
If FileExists(Ls_outputpath) Then
	FileDelete(Ls_outputpath)//删除掉原先的文件
End if
//
ll_row = filetoBase64(ls_filepath,Ls_outputpath)//base64编码
If ll_row = 0 Then
	//messageBox('提示','Base64编码文件成功!')
Else
	messageBox('提示','Base64编码文件失败!【'+is_filename+'】')
	return
End If

If wf_get_blob(Ls_outputpath,lb_base64String) = true then
else
	messageBox('提示','读取失败!')
	return
End If

获取到编码后的base64数据函数

//==============================================================================
// 函数: w_gxnnyb_tsxmba::wf_get_blob()
//------------------------------------------------------------------------------
// 描述: 
//------------------------------------------------------------------------------
// 参数: 
//		      	string	as_filepath		
//		ref   	blob       	ab_blob		
//------------------------------------------------------------------------------
// 返回值:  boolean
//------------------------------------------------------------------------------
// 作者:	xyg		日期: 2019.12.12
//------------------------------------------------------------------------------
// 修改历史: 
//	
//------------------------------------------------------------------------------
// Copyright (c) 2000-2013 KingT Software Co., Ltd., All rights reserved.
//==============================================================================

string export_filename
integer li_FileNum, loops, i
long flen, bytes_read, new_pos,ll_filelen
blob b, tot_b
string str=''
// Set a wait cursor
//If Getfilesavename("选择保存路径",ls_pic1name,export_filename,&
//"zip","zip Files (*.zip),*.zip,"+ "jpg Files (*.jpg),*.jpg,"+"ALL Files (*.*),*.*") <> 1 Then return false
SetPointer(HourGlass!)
// Get the file length, and open the file
flen = FileLength(as_filepath)
ll_filelen = flen
li_FileNum = FileOpen(as_filepath,StreamMode!, Read!, LockRead!)
// Determine how many times to call FileRead
IF flen > 32765 THEN
   IF Mod(flen, 32765) = 0 THEN
      loops = flen/32765
   ELSE
      loops = (flen/32765) + 1
   END IF
ELSE
   loops = 1
END IF
// Read the file
new_pos = 1
FOR i = 1 to loops
    bytes_read = FileRead(li_FileNum, b)
	 Choose Case bytes_read
		Case -100
			Exit
		Case 0
			MessageBox('提示','读取文件出错!')
			Return False
		Case -1
			MessageBox('提示','读取文件出错!')
			Return False
	End Choose
   tot_b = tot_b + b
NEXT
FileClose(li_FileNum)
ab_blob = tot_b
Return true

PB外部函数声明(注意PB使用的时候不要system library,这个DLL只是代打工,不是正式员工,不然调用无响应,我还被坑了好久

//function int filetoBase64ToString(string filePath, ref string content) library "pbBase64Func.dll"alias for "filetoBase64ToString" 
//filePath:为base64加密文本路径   outPut:base64解密后的文件路径
function int base64ToFile(string filePath, string outPut) library "pbBase64Func.dll" alias for "base64ToFile" 
//content:base64字符串   outPut:base64解密后的路径 
function int base64ContentToFile(string content, string outPut) library "pbBase64Func.dll" alias for "base64ContentToFile" 
//filePath:要加密的文件路径   output:转成base64后的编码字符串路径
function int filetoBase64(string filePath, string outPut) library "pbBase64Func.dll" alias for "filetoBase64"

lib.h 更新(2021-06-24更新 原先的有点问题重新改了下,base64编码出来后面会跟出许多\0)

#include <Windows.h>
#include <iostream>
#include <string>
#include <fstream>
#include <assert.h>
#include "time.h"

bool is_readable(const std::string & file)
{
	wchar_t szBuffer[1024];
	OemToCharW(file.c_str(), szBuffer);
	std::ifstream fichier(szBuffer);
	return !fichier.fail();
}
long filesize(FILE *stream)
{
	long curpos, length;

	curpos = ftell(stream);
	fseek(stream, 0L, SEEK_END);
	length = ftell(stream);
	fseek(stream, curpos, SEEK_SET);
	return length;

}
// Base 64

static const std::string base64_chars =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789+/";


static inline bool is_base64(unsigned char c) {
	return (isalnum(c) || (c == '+') || (c == '/'));
}

std::string base64_encode(unsigned char const* bytes_to_encode, unsigned int in_len)
{
	std::string ret;
	int i = 0;
	int j = 0;
	unsigned char char_array_3[3];
	unsigned char char_array_4[4];

	while (in_len--) {
		char_array_3[i++] = *(bytes_to_encode++);
		if (i == 3) {
			char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
			char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
			char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);
			char_array_4[3] = char_array_3[2] & 0x3f;

			for (i = 0; (i < 4); i++)
				ret += base64_chars[char_array_4[i]];
			i = 0;
		}
	}

	if (i)
	{
		for (j = i; j < 3; j++)
			char_array_3[j] = '\0';

		char_array_4[0] = (char_array_3[0] & 0xfc) >> 2;
		char_array_4[1] = ((char_array_3[0] & 0x03) << 4) + ((char_array_3[1] & 0xf0) >> 4);
		char_array_4[2] = ((char_array_3[1] & 0x0f) << 2) + ((char_array_3[2] & 0xc0) >> 6);

		for (j = 0; (j < i + 1); j++)
			ret += base64_chars[char_array_4[j]];

		while ((i++ < 3))
			ret += '=';

	}

	return ret;

}

std::string hex_string(std::string hexstr)
{
	std::string str = "";
	str.resize((hexstr.size() + 1) / 2);
	for (size_t i = 0, j = 0; i < str.size(); i++, j++)
	{
		char at = '@';
		str[i] = (hexstr[j] & at ? hexstr[j] + 9 : hexstr[j]) << 4, j++;
		str[i] |= (hexstr[j] & at ? hexstr[j] + 9 : hexstr[j]) & 0xF;
	}
	return str;
}

std::string string_hex(std::string str, const bool capital = false)
{
	std::string hexstr = "";
	hexstr.resize(str.size() * 2);
	static const char a = capital ? 0x40 : 0x60;
	for (size_t i = 0; i < str.size(); i++)
	{
		char c = (str[i] >> 4) & 0xF;
		hexstr[i * 2] = c > 9 ? (c - 9) | a : c | '0';
		hexstr[i * 2 + 1] = (str[i] & 0xF) > 9 ? (str[i] - 9) & 0xF | a : str[i] & 0xF | '0';
	}
	return hexstr;
}

std::string char_to_string(char x[], int size_recv)
{
	int num_car = 0;
	int stop_while = size_recv;
	std::string output = "";

	while (stop_while > 0)
	{
		output += x[num_car];
		/*if (x[num_car] == '\0')
			break;*/
		if (num_car < size_recv) { num_car++; }

		stop_while--;
	}

	return output;
}

std::string str_base64_encode(std::string str)
{
	return base64_encode(reinterpret_cast<const unsigned char*>(str.c_str()), str.length());
}

std::string base64_decode(std::string const& encoded_string) {
	int in_len = encoded_string.size();
	int i = 0;
	int j = 0;
	int in_ = 0;
	unsigned char char_array_4[4], char_array_3[3];
	std::string ret;

	while (in_len-- && (encoded_string[in_] != '=') && is_base64(encoded_string[in_])) {
		char_array_4[i++] = encoded_string[in_]; in_++;
		if (i == 4) {
			for (i = 0; i < 4; i++)
				char_array_4[i] = base64_chars.find(char_array_4[i]);

			char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
			char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);
			char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3];

			for (i = 0; (i < 3); i++)
				ret += char_array_3[i];
			i = 0;
		}
	}

	if (i) {
		for (j = 0; j < i; j++)
			char_array_4[j] = base64_chars.find(char_array_4[j]);

		char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4);
		char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2);

		for (j = 0; (j < i - 1); j++) ret += char_array_3[j];
	}

	return ret;
}

// Decode file

int decode_file_base64(std::string path_in, std::string path_out)
{
	std::string path_file = path_in;

	if (is_readable(path_file))
	{
		FILE *file_input;
		FILE *file_output;
		fopen_s(&file_input, path_file.c_str(), "ab+");
		fopen_s(&file_output, path_out.c_str(), "ab+");

		char buff_read[1024];

		if (file_input != NULL)
		{
			long length = filesize(file_input);
			std::string str_encode_buff = "";
			size_t rtn = 1;
			while (rtn)
			{
				rtn = fread(buff_read, sizeof(buff_read), 1, file_input);
				str_encode_buff += char_to_string(buff_read, min(sizeof(buff_read), length));
				length = length - sizeof(buff_read);
				memset(buff_read, 0, sizeof(buff_read));
			}
			str_encode_buff = base64_decode(str_encode_buff);

			fwrite(str_encode_buff.c_str(), strlen(string_hex(str_encode_buff).c_str()) / 2, 1, file_output);

			fclose(file_input);
			fclose(file_output);
		}
		delete[] buff_read;
	}
	return 0;
}

int encode_file_base64(std::string path_in, std::string path_out)
{
	std::string path_file = path_in;

	if (is_readable(path_file))
	{
		FILE *file_input;
		FILE *file_output;

		fopen_s(&file_input, path_file.c_str(), "ab+");
		fopen_s(&file_output, path_out.c_str(), "ab+");

		char buff_read[1024];

		if (file_input != NULL)
		{
			std::string str_encode_buff = "";
			size_t rtn = 1;
			long length = filesize(file_input);
			memset(buff_read, 0, sizeof(buff_read));
			while (rtn)
			{
				rtn = fread(buff_read, sizeof(buff_read), 1, file_input);
				str_encode_buff += char_to_string(buff_read, min(sizeof(buff_read), length));
				length = length - sizeof(buff_read);
				memset(buff_read, 0, sizeof(buff_read));
			}

			str_encode_buff = str_base64_encode(str_encode_buff);
			fwrite(str_encode_buff.c_str(), strlen(string_hex(str_encode_buff).c_str()) / 2, 1, file_output);

			fclose(file_input);
			fclose(file_output);
		}
		delete[] buff_read;
	}

	return 0;
}
std::string getTime(){
	time_t t = time(0);
	char ch[64];
	strftime(ch, sizeof(ch), "%Y-%m-%d %H-%M-%S %S", localtime(&t));
	return std::string(ch) ;
}


int savelog(std::string as_content,std::string outLogPath){
	if (outLogPath.empty()) return -1;
	//return 0;
	as_content = as_content + '\r'+'\n';
	FILE *file_input;
	//std::string path_in = "E:\\ProgramFile_xyg\\_Net_Program\\pbBase64Func\\KingTPowerBase64\\log\\20210623.log";
	fopen_s(&file_input, outLogPath.c_str(), "ab+");
	if (file_input != NULL){
		std::string time = getTime()+":";
		fwrite(time.c_str(), strlen(string_hex(time).c_str()) / 2, 1, file_input);
		fwrite(as_content.c_str(), strlen(string_hex(as_content).c_str()) / 2, 1, file_input);
	}
	fclose(file_input);
	return 0;
}
/*
int decode_jsonfile_base64(std::string path_in, std::string path_out, std::string jsonKey,std::string outlog){
	savelog("path_in=" + path_in, outlog);
	savelog("path_out=" + path_out, outlog);
	savelog("jsonKey=" + jsonKey, outlog);
	if (is_readable(path_in)){
		FILE *file_input;
		fopen_s(&file_input, path_in.c_str(), "ab+");
		char buff_read[1024];
		std::string str_encode_buff = "";
		size_t rtn = 1;
		long length = filesize(file_input);
		memset(buff_read, 0, sizeof(buff_read));
		savelog("decode_jsonfile_base64开始进入!", outlog);
		if (file_input != NULL)
		{
			while (rtn)
			{
				rtn = fread(buff_read, sizeof(buff_read), 1, file_input);
				str_encode_buff += char_to_string(buff_read, min(sizeof(buff_read), length));
				length = length - sizeof(buff_read);
				memset(buff_read, 0, sizeof(buff_read));
			}
		}
		fclose(file_input);
		delete[] buff_read;
		savelog("file_input文件读取成功!", outlog);
		JsonValue json = JsonParser::Parse(str_encode_buff.c_str());
		savelog("Json实例化成功!", outlog);
		unsigned int items = json.GetLength();

		if (items <= 0) return -5;

		bool lb_flags = false;
		for (UINT i = 0, l = json.GetLength(); i < l; ++i)
		{
			JString name = json.GetName(i);
			//JString type = json.Get(name)->GetType();
			if (name.Equals(jsonKey.c_str())){
				JString value = json.Get(name)->GetString();
				str_encode_buff = value;
				lb_flags = true;
				break;
			}
			//Print("Name=[%-7s] Type=[%-7s] StringValue=\"%s\"", (char*)name, (char*)type, (char*)value);
		}
		savelog("Json取值结束!", outlog);
		if (lb_flags == false) return -3;
		savelog("Json取值成功!", outlog);
		//读取完成进行base64解码
		str_encode_buff = base64_decode(str_encode_buff);
		savelog("Base64解码成功!", outlog);
		FILE *file_output;
		fopen_s(&file_output, path_out.c_str(), "ab+");
		if (file_output != NULL){
			fwrite(str_encode_buff.c_str(), strlen(string_hex(str_encode_buff).c_str()) / 2, 1, file_output);
		}
		fclose(file_output);
		savelog("Base64解码后写入文件成功!", outlog);
		//std::cout << "length=" + str_encode_buff.length() << std::endl;
	}
	else{
		savelog("failure=", outlog);
		return -1;
	}
	return 0;
}*/

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Base64编码是一种针对字节编码,它将字节流转换为一种可以传输的字符串格式。例如,将字节流"Hello World"转换为Base64编码的字符串形式可以为"SGVsbG8gV29ybGQ="。 ### 回答2: Base64是一种将二进制数据编码为ASCII字符的编码方式。它通常用于在网络传输中将二进制数据以文本形式进行传输和存储。 在C语言中,我们可以使用标准库中的函数实现Base64编码示例。下面是一个使用C语言实现Base64编码的简单示例代码: ```c #include <stdio.h> #include <stdlib.h> #include <string.h> const char base64_table[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"; char *base64_encode(const unsigned char *data, int size) { int i, j; int encode_size = 4 * ((size + 2) / 3); char *encoded_data = malloc(encode_size + 1); if (encoded_data == NULL) { return NULL; } for (i = 0, j = 0; i < size; i += 3, j += 4) { unsigned char a = i < size ? data[i] : 0; unsigned char b = i + 1 < size ? data[i + 1] : 0; unsigned char c = i + 2 < size ? data[i + 2] : 0; encoded_data[j] = base64_table[a >> 2]; encoded_data[j + 1] = base64_table[((a & 0x03) << 4) | (b >> 4)]; encoded_data[j + 2] = (i + 1 < size) ? base64_table[((b & 0x0f) << 2) | (c >> 6)] : '='; encoded_data[j + 3] = (i + 2 < size) ? base64_table[c & 0x3f] : '='; } encoded_data[encode_size] = '\0'; return encoded_data; } int main() { unsigned char data[] = "Hello, World!"; int size = strlen((char*)data); char *encoded_data = base64_encode(data, size); if (encoded_data == NULL) { printf("Base64 encoding failed. No memory available.\n"); return 1; } printf("Base64 encoded data: %s\n", encoded_data); free(encoded_data); return 0; } ``` 在上述代码中,我们定义了一个`base64_table`字符数组,用于映射二进制数据到Base64编码字符。然后,我们定义了一个`base64_encode()`函数,该函数接收需要编码的数据和数据尺寸作为输入,并返回编码后的数据。 在`main()`函数中,我们定义了一个示例数据`data`,并调用`base64_encode()`函数对其进行编码。然后,我们打印出编码后的数据。 请注意,上述代码只是一个简单的Base64编码示例,并不包括对边界情况的处理和错误检查。实际使用中,可能需要进行更多的错误处理和边界条件检查。 ### 回答3: Base64编码是一种将二进制数据转换为可打印字符的编码方式。它将3个字节的二进制数据编码为4个可打印字符,以实现数据在传输过程中的可读性和可靠性。 要实现Base64编码,我们需要按照以下步骤进行操作: 1. 准备Base64编码表:将A~Z、a~z、0~9这62个字符和"+"、"/"两个字符组成一个长度为64的字符数组。在实际编码中,有些情况下会使用"="字符来填充不足4字节的情况。 2. 将需要编码的数据分割成每3个字节一组。如果数据不足3个字节,需要进行填充至3个字节。 3. 将每组3个字节的二进制数据转换成4个Base64字符的编码。 4. 如果有填充字符,则将对应的填充字符添加到编码结果中。 下面是一个Base64编码的示例: 假设我们需要编码的数据是"Hello, World!"。 1. 将字符串转换成对应的字节数组:[72, 101, 108, 108, 111, 44, 32, 87, 111, 114, 108, 100, 33]。 2. 将字节数组分组:[72, 101, 108]、[108, 111, 44]、[32, 87, 111]、[114, 108, 100]、[33]。 3. 将每组3个字节的二进制数据转换成4个Base64字符的编码: 第一组:01101000 01100101 01101100 -> 011010 000110 010101 101100 -> i Wxu 第二组:01101100 01101111 00101100 -> 011011 000110 111100 101100 -> bGVv 第三组:00100000 01010111 01101111 -> 001000 000101 011110 110000 -> IAXj 第四组:01110010 01101100 01100100 -> 011100 100110 110100 010000 -> chrS 第五组:00100001 00000000 00000000 -> 001000 010000 000000 000000 -> IAAAAAAAAA 编码结果为:iWxubGVvIAXjchrSIAAAAAAAAA== 通过以上步骤,我们成功地将数据"Hello, World!"进行了Base64编码
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值