base64

来源:互联网 发布:java发送带图片的邮件 编辑:程序博客网 时间:2024/09/21 09:23

base64解码和编码范例(iPhone和Mac通用)   (在iOS代码库中浏览本帖)

请参考fly的blog
http://www.flyblog.info/catprogramming/376.html
和我的blog
http://www.cocoabob.net/?p=7

都是使用了一个叫Jonathan Wight的人在2003年3月18日写的Base64Transcoder。传播过程中丢失了版权信息,也把它改成了ObjC的文件命名。

另外,请不要用这个链接里所说的base64编解码器,测试下来,发现只能对字母和数字以及符号进行编解码,这种编解码器没法用来处理纯数据,只能用来处理纯文本。(希望我没有测试错误~)
http://www.cocoachina.com/bbs/read.php?tid-6485-page-e-fpage-1.html

示范代码在这
?
1
2
3
4
5
6
7
8
NSString *stringValue = /*the UTF8 string parsed from xml data*/
Byte inputData[[stringValue lengthOfBytesUsingEncoding:NSUTF8StringEncoding]];//prepare a Byte[]
[[stringValue dataUsingEncoding:NSUTF8StringEncoding] getBytes:inputData];//get the pointer of the data
size_t inputDataSize = (size_t)[stringValue length];
size_t outputDataSize = EstimateBas64DecodedDataSize(inputDataSize);//calculate the decoded data size
Byte outputData[outputDataSize];//prepare a Byte[] for the decoded data
Base64DecodeData(inputData, inputDataSize, outputData, &outputDataSize);//decode the data
NSData *theData = [[NSData alloc] initWithBytes:outputData length:outputDataSize];//create a NSData object from the decoded data
[ 此帖被bobgreen在2010-08-16 18:21重新编辑 ]

附件: base64.zip (3 K) 下载次数:1893
0 0
原创粉丝点击