base64_encode 如何传入uint8_t *data进行编码
时间: 2023-08-05 19:42:41 浏览: 54
您可以将 `uint8_t` 数组转换为 `std::string`,然后使用 `base64_encode` 函数进行编码。下面是一个示例代码:
```c++
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <string>
#include <iostream>
std::string base64_encode(const std::string& input) {
BIO *bio, *b64;
BUF_MEM *bufferPtr;
bio = BIO_new(BIO_s_mem());
b64 = BIO_new(BIO_f_base64());
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
BIO_write(bio, input.c_str(), input.size());
BIO_flush(bio);
BIO_get_mem_ptr(bio, &bufferPtr);
std::string output(bufferPtr->data, bufferPtr->length);
BIO_free_all(bio);
return output;
}
int main() {
uint8_t data[] = {0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x2c, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64, 0x21};
std::string input(reinterpret_cast<char const*>(data), sizeof(data));
std::string encoded = base64_encode(input);
std::cout << encoded << std::endl;
return 0;
}
```
该示例中的 `main` 函数将 `uint8_t` 数组转换为 `std::string`,然后使用 `base64_encode` 函数对其进行编码,并将编码后的字符串输出到控制台。