IOS Base64文件转换

1.uiimage转成base64
+ (NSString *)ImageTobase64:(NSString *)imagename {
    UIImage *_originImage = [UIImage imageNamed:imagename];

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用C++将base64转换文件的示例代码: ```cpp #include <iostream> #include <fstream> #include <sstream> #include <string> #include <vector> // Base64解码函数 std::string base64_decode(const std::string& encoded_string) { // Base64字符集 const std::string base64_chars = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" "abcdefghijklmnopqrstuvwxyz" "0123456789+/"; std::string decoded_string; int in_len = encoded_string.size(); int i = 0; int j = 0; int in_ = 0; unsigned char char_array_4[4], char_array_3[3]; while (in_len-- && (encoded_string[in_] != '=') && (is_base64(encoded_string[in_]))) { char_array_4[i++] = encoded_string[in_]; in_++; if (i == 4) { for (i = 0; i < 4; i++) char_array_4[i] = base64_chars.find(char_array_4[i]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (i = 0; (i < 3); i++) decoded_string += char_array_3[i]; i = 0; } } if (i) { for (j = i; j < 4; j++) char_array_4[j] = 0; for (j = 0; j < 4; j++) char_array_4[j] = base64_chars.find(char_array_4[j]); char_array_3[0] = (char_array_4[0] << 2) + ((char_array_4[1] & 0x30) >> 4); char_array_3[1] = ((char_array_4[1] & 0xf) << 4) + ((char_array_4[2] & 0x3c) >> 2); char_array_3[2] = ((char_array_4[2] & 0x3) << 6) + char_array_4[3]; for (j = 0; (j < i - 1); j++) decoded_string += char_array_3[j]; } return decoded_string; } // 将base64转换文件 void base64_to_file(const std::string& base64_string, const std::string& file_path) { std::ofstream file(file_path, std::ios::binary); if (file) { std::string decoded_string = base64_decode(base64_string); file.write(decoded_string.c_str(), decoded_string.length()); file.close(); std::cout << "Base64 string has been converted to file: " << file_path << std::endl; } else { std::cout << "Failed to open file: " << file_path << std::endl; } } int main() { std::string base64_string = "TWFueSBoYW5kcyBtYWtlIGxpZ2h0IHdvcmsu"; std::string file_path = "output.txt"; base64_to_file(base64_string, file_path); return 0; } ``` 这段代码首先定义了一个`base64_decode`函数,用于将base64编码的字符串解码为原始字符串。然后,定义了一个`base64_to_file`函数,该函数接受base64编码的字符串和文件路径作为参数,将base64字符串解码并将结果写入文件。最后,在`main`函数中,我们提供了一个base64编码的字符串和输出文件路径,并调用`base64_to_file`函数将base64字符串转换文件

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值