C++ 如何将二进制字符串转换为 base64 编码的数据
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6385319/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 20:01:01 来源:igfitidea点击:
How to convert a binary string into base64 encoded data
提问by Balamurugan
I am receiving binary data in a string. I want to encode that into Base64. Is there any class to do that operation (I want an API).
我正在接收字符串中的二进制数据。我想将其编码为 Base64。是否有任何类可以执行该操作(我想要一个 API)。
采纳答案by king_nak
回答by Ruslan Garipov
CryptBinaryToString...if you target to Windows platform
CryptBinaryToString...如果你的目标是 Windows 平台
Here is a little sample:
这是一个小示例:
#include <Windows.h>
#pragma comment(lib, "crypt32.lib")
int main()
{
LPCSTR pszSource = "Man is distinguished, not only by his reason, but ...";
DWORD nDestinationSize;
if (CryptBinaryToString(reinterpret_cast<const BYTE*> (pszSource), strlen(pszSource), CRYPT_STRING_BASE64, nullptr, &nDestinationSize))
{
LPTSTR pszDestination = static_cast<LPTSTR> (HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE, nDestinationSize * sizeof(TCHAR)));
if (pszDestination)
{
if (CryptBinaryToString(reinterpret_cast<const BYTE*> (pszSource), strlen(pszSource), CRYPT_STRING_BASE64, pszDestination, &nDestinationSize))
{
// Succeeded: 'pszDestination' is 'pszSource' encoded to base64.
}
HeapFree(GetProcessHeap(), HEAP_NO_SERIALIZE, pszDestination);
}
}
return 0;
}