我看过很多关于如何在 Objective C 中解码 Base64 编码字符串的 Stack Overflow 帖子,但我仍然遇到问题。
本质上,我正在尝试将 Python 脚本移植到 Objective C。Python 代码行是:
zlib.decompress(base64.urlsafe_b64decode(string))
当我通过 Python 版本的 base64.urlsafe_b64decode 运行“string”时,结果是正确的,然后可以正确地进行 Zlib 解压缩。当我通过 Objective C Base64 解码器的任何变体运行“字符串”时,它有点工作,但结果不一样并且 Zlib 解压缩失败。
URL 安全的 Base 64 解码和 SO 上广泛可用的代码之间有区别吗?如果任何人以前遇到过这样的困难,我们将不胜感激任何关于该怎么做的见解。
谢谢,太好了!
编辑 1: 我使用了找到的 Base 64 代码 here .可以找到原始数据(仍然是Base64编码)here ,可以找到生成的Objective-C here , 可以找到生成的 Python here .理想情况下,我希望 Objective C 代码解码为与 Python 脚本相同的文本。
最佳答案
特别感谢 Graham 指出了 RFC 的差异,我得以解决问题。如果以后有人遇到这种情况,请按照以下方法解决:
- 从 here 下载 NSData+Base64 代码。
- 在
NSData+Base64.m
中,您需要将查找表更改为以下内容:
// // Mapping from 6 bit pattern to ASCII character. // static unsigned char base64EncodeLookup[65] = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; // // Definition for "masked-out" areas of the base64DecodeLookup mapping // #define xx 65 // // Mapping from ASCII character to 6 bit pattern. // static unsigned char base64DecodeLookup[256] = { xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, 62, xx, xx, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, xx, xx, xx, xx, xx, xx, xx, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, xx, xx, xx, xx, 63, xx, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, xx, };
这将使 Objective C 的解码等同于 Python 的解码。
关于python - Objective-C 中的 URL 安全 Base64,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11106393/