为什么这个base64解码/编码功能无法正常工作?

时间:2018-04-11 08:22:41

标签: c++ base64

我正在使用here中的base64函数来尝试运行一个简单的程序。我想使用以下代码将二进制文件转换为base64:

int x = 1;
std::string data = base64_encode(reinterpret_cast<unsigned char *>((void *)&x), 4);
std::string out = base64_decode(data);
int y = reinterpret_cast<int>(out.data());

调用encode函数,并生成此字符串"AQAAAA=="。根据我的理解,如果解码并转换为字节,这将转换为01 00 00 0(我实际上并不理解为什么它的7个字节)。当调用decode函数时,我期望01 00 00 00,然后将reinterpret_castable重新变为整数1,而是得到"\001\000\000",这不是我所期望的。我尝试增加编码函数的第二个参数,有趣的是,它在解码后给了我"\001\000\000\000"的正确答案。

1 个答案:

答案 0 :(得分:2)

假设您使用this code作为base64,我怀疑以下内容将起作用:

saveAction.setValue(zdjecieGlowne.withRenderingMode(UIImageRenderingMode.alwaysOriginal), forKey: "image")

在执行解码逻辑的任何一方,它应该在执行此转换之前验证int x = 1; std::string data = base64_encode(reinterpret_cast<unsigned char *>(&x), 4); std::string out = base64_decode(data); int y= *(reinterpret_cast<int*>(out.c_str()));