简体   繁体   English

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

[英]How to convert a binary string into base64 encoded data

I am receiving binary data in a string.我正在接收字符串中的二进制数据。 I want to encode that into Base64.我想将其编码为 Base64。 Is there any class to do that operation (I want an API).是否有任何类可以执行该操作(我想要一个 API)。

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

A quick google search for c++ base64 gives you these links: 快速谷歌搜索c++ base64为您提供以下链接:
1 1
2 2
3 3

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM