gpt4 book ai didi

c++ - 如何将二进制字符串转换为base64编码的数据

转载 作者:行者123 更新时间:2023-12-02 10:30:59 24 4
gpt4 key购买 nike

我正在接收字符串中的二进制数据。我想将其编码为Base64。是否有任何类可以执行该操作(我需要API)。

最佳答案

CryptBinaryToString
...如果您定位到Windows平台

这是一个小样本:

#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;
}

关于c++ - 如何将二进制字符串转换为base64编码的数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62293965/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com