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

问题描述:

我正在接收字符串中的二进制数据。我想将它编码到Base64中。有没有任何类可以做这个操作(我想要一个API)。如何将二进制字符串转换为base64编码数据

+0

你熟悉哪种语言? – jforberg 2011-06-17 11:52:13

快速谷歌搜索c++ base64为您提供这些链接:
1
2
3

+2

将来,请至少发布有关答案的最少量信息,不要仅链接到它们。由于网络的动态性,链接随着年龄的增长而变化,然后你的帖子变得毫无用处。 – nmg49 2017-04-20 14:39:09

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

尽管这个链接可能回答这个问题,但最好在这里包含答案的重要部分,并提供供参考的链接。如果链接页面更改,则仅链接答案可能会失效。 – CodeMouse92 2015-10-06 14:01:24