string转base64
时间: 2023-07-04 22:11:41 浏览: 81
在C++中,可以使用base64_encode函数将字符串转换为base64编码。以下是一个示例代码:
```c++
#include <iostream>
#include <string>
#include <openssl/bio.h>
#include <openssl/evp.h>
#include <openssl/buffer.h>
std::string base64_encode(const std::string& input) {
BIO *b64, *bio;
BUF_MEM *bptr = BUF_MEM_new();
b64 = BIO_new(BIO_f_base64());
bio = BIO_new(BIO_s_mem());
bio = BIO_push(b64, bio);
BIO_set_flags(bio, BIO_FLAGS_BASE64_NO_NL);
BIO_write(bio, input.c_str(), input.length());
BIO_flush(bio);
BIO_get_mem_ptr(bio, &bptr);
std::string output(bptr->data, bptr->length);
BIO_free_all(bio);
BUF_MEM_free(bptr);
return output;
}
int main() {
std::string input = "Hello, world!";
std::string output = base64_encode(input);
std::cout << "Base64 encoded string: " << output << std::endl;
return 0;
}
```
输出结果为:
```
Base64 encoded string: SGVsbG8sIHdvcmxkIQ==
```