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

时间:2011-06-17 11:50:37

标签: c++ class encoding base64 binary-data

我在字符串中接收二进制数据。我想将其编码到Base64中。是否有任何类来执行该操作(我想要一个API)。

2 个答案:

答案 0 :(得分:3)

c++ base64的快速谷歌搜索为您提供以下链接:
1
2
3

答案 1 :(得分:3)

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