How to convert a binary string into base64 encoded data

后端 未结 1 1017
渐次进展
渐次进展 2020-12-11 08:09

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).

1条回答
  •  囚心锁ツ
    2020-12-11 08:52

    CryptBinaryToString ...if you target to Windows platform

    Here is a little sample:

    #include 
    
    #pragma comment(lib, "crypt32.lib")
    
    int main()
    {
        LPCSTR pszSource = "Man is distinguished, not only by his reason, but ...";
        DWORD nDestinationSize;
        if (CryptBinaryToString(reinterpret_cast (pszSource), strlen(pszSource), CRYPT_STRING_BASE64, nullptr, &nDestinationSize))
        {
            LPTSTR pszDestination = static_cast (HeapAlloc(GetProcessHeap(), HEAP_NO_SERIALIZE, nDestinationSize * sizeof(TCHAR)));
            if (pszDestination)
            {
                if (CryptBinaryToString(reinterpret_cast (pszSource), strlen(pszSource), CRYPT_STRING_BASE64, pszDestination, &nDestinationSize))
                {
                    // Succeeded: 'pszDestination' is 'pszSource' encoded to base64.
                }
                HeapFree(GetProcessHeap(), HEAP_NO_SERIALIZE, pszDestination);
            }
        }
        return 0;
    }
    

    0 讨论(0)
提交回复
热议问题