使用GTMBase64编码解码字符串

使用GTMBase64编码解码字符串


说明:BASE64不是用来加密的。你看看经过BASE64编码后的字符串,全部都是由标准键盘上面的常规字符组成,这样编码后的字符串在网关之间传递不会产生UNICODE字符串不能识别或者丢失的现象。你再仔细研究下EMAIL就会发现其实EMAIL就是用base64编码过后再发送的。然后接收的时候再还原。
    还有一种情况下用BASE64编码也很好,比如一个图片文件,或者其他任何二进制文件。我可以把它编码成字符串。这样用XML或者数据库就能直接以文本的方式来存储这些文件了。
<pre id="answer-content-418531940" class="answer-text mb-10" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; padding: 0px; font-family: arial, 'courier new', courier, 宋体, monospace; background-color: rgb(255, 255, 255);">base64是一种编码方式,编码算法完全公开,所以逆向解码即可

 
  
</pre><pre id="best-content-419398544" class="best-text mb-10" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; padding: 0px; font-family: arial, 'courier new', courier, 宋体, monospace; color: rgb(51, 51, 51); line-height: 24px; background-color: rgb(241, 254, 221);"><p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; line-height: 26px;">使用GTMBase64需要在工程中加入三个文件</p><div class="cnblogs_code" style="font-family: Arial; line-height: 26px;"><pre style="white-space: pre-wrap; word-wrap: break-word;"><a target=_blank href="http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/GTMDefines.h?r=87" target="_blank" style="color: rgb(255, 153, 0); text-decoration: none;">GTMDefines.h</a>
<a target=_blank href="http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMBase64.h?r=87" target="_blank" style="color: rgb(255, 153, 0); text-decoration: none;">GTMBase64.h</a>
<a target=_blank href="http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/GTMBase64.m?r=87" target="_blank" style="color: rgb(255, 153, 0); text-decoration: none;">GTMBase64.m</a>

你可以在这里找到这三个文件
<a target=_blank href="http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/?r=87" style="color: rgb(255, 153, 0); text-decoration: none;">http://code.google.com/p/google-toolbox-for-mac/source/browse/trunk/Foundation/?r=87</a>

 
 
<p style="margin-top: 0px; margin-bottom: 0px; padding-top: 0px; padding-bottom: 0px; font-family: Arial; line-height: 26px;">示例代码:</p><div class="cnblogs_code" style="font-family: Arial; line-height: 26px;"><div class="cnblogs_code_toolbar"><span class="cnblogs_code_copy"><a target=_blank title="复制代码" style="color: rgb(255, 153, 0);"><img src="http://common.cnblogs.com/images/copycode.gif" alt="复制代码" style="border: none; max-width: 100%;" /></a></span></div><pre style="white-space: pre-wrap; word-wrap: break-word;"><span style="color: rgb(0, 0, 255);">#import</span> <span style="color: rgb(128, 0, 0);">"</span><span style="color: rgb(128, 0, 0);">GTMBase64.h</span><span style="color: rgb(128, 0, 0);">"</span>

- (<span style="color: rgb(0, 0, 255);">void</span>)testExample
{
    NSData *data = [<span style="color: rgb(128, 0, 0);">@"</span><span style="color: rgb(128, 0, 0);">HelloWorld</span><span style="color: rgb(128, 0, 0);">"</span> dataUsingEncoding:NSUTF8StringEncoding allowLossyConversion:YES];
    NSString* encoded = [[NSString alloc] initWithData:[GTMBase64 encodeData:data] encoding:NSUTF8StringEncoding]; 
    NSLog(<span style="color: rgb(128, 0, 0);">@"</span><span style="color: rgb(128, 0, 0);">encoded:%@</span><span style="color: rgb(128, 0, 0);">"</span>, encoded);
    NSString* decoded = [[NSString alloc] initWithData:[GTMBase64 decodeString:encoded] encoding:NSUTF8StringEncoding];
    NSLog(<span style="color: rgb(128, 0, 0);">@"</span><span style="color: rgb(128, 0, 0);">decoded:%@</span><span style="color: rgb(128, 0, 0);">"</span>, decoded);
    [encoded release];
    [decoded release];
}
复制代码

 

输出:

2012-04-10 12:13:10.121 fs_test[7370:b603] encoded:SGVsbG9Xb3JsZA==
2012-04-10 12:13:10.122 fs_test[7370:b603] decoded:HelloWorld

 
 
</pre><pre id="best-content-419398544" class="best-text mb-10" name="code" style="white-space: pre-wrap; word-wrap: break-word; margin-top: 0px; margin-bottom: 10px; padding: 0px; font-family: arial, 'courier new', courier, 宋体, monospace; color: rgb(51, 51, 51); line-height: 24px; background-color: rgb(241, 254, 221);">使用示例:
引入头文件:
#import "GTMBase64.h"
UIImage *image;
NSData *data= UIImagePNGRepresentation(image);
    if([data length]>0){
        data = [GTMBase64 encodeData:data];                 //编码
    }
   if([data length]>0){
        data = [GTMBase64 decodeData:data];                  //解码
   }

你可能感兴趣的:(使用GTMBase64编码解码字符串)