iOS 7: Base64 Encode and Decode NSData and NSString Objects

iOS 7: Base64 Encode and Decode NSData and NSString Objects

FRI, JAN 24

CORE SERVICES

TWEET

With the release of iOS 7, Apple added support for encoding and decoding data using Base64. In this post we will walk through two examples using Base64 to encode and decode both NSData and NSString objects.

First, we will create an NSString object that is generated by Base64 encoding an NSData object. This will be followed by decoding the Base64 NSString back into an NSData object. We will display the NSString data, both encoded and decoded to make sure all is well.

The second example will encode and decode NSData to/from Base64. This example is relevant if you have an NSData object that needs to be Base64 encoded, or you need to decode a Base64 NSData object (for whatever reason).

Create a Base64 Encoded NSString Object

Let’s begin by encoding an NSData object into Base64 and returning an NSString object:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
// Create NSData object
NSData *nsdata = [@"iOS Developer Tips encoded in Base64"
  dataUsingEncoding:NSUTF8StringEncoding];
 
// Get NSString from NSData object in Base64
NSString *base64Encoded = [nsdata base64EncodedStringWithOptions:0];
 
// Print the Base64 encoded string
NSLog(@"Encoded: %@", base64Encoded);
 
// Let‘s go the other way...
 
// NSData from the Base64 encoded str
NSData *nsdataFromBase64String = [[NSData alloc]
  initWithBase64EncodedString:base64Encoded options:0];
 
// Decoded NSString from the NSData
NSString *base64Decoded = [[NSString alloc]
  initWithData:nsdataFromBase64String encoding:NSUTF8StringEncoding];
NSLog(@"Decoded: %@", base64Decoded);

On line 2 we create the NSData to encode. Line 6 encodes the data and returns an NSString object.

To go back the other way, from a Base64 encoded NSString object to an NSData object is as easy as calling the method initWithBase64EncodedString, passing in the Base64 encoded NSString (lines 14-15).

Lines 18-20 go from the Base64 NSData object back to an NSString (which is how we started this example, from a string).

The output looks as follows:

Encoded: aU9TIERldmVsb3BlciBUaXBzIGVuY29kZWQgaW4gQmFzZTY0
Decoded: iOS Developer Tips encoded in Base64

Base64 Encode an NSData Object

There’s a good chance the NSString conversions (above) may be more about debugging than a day-to-day need to Base64 encoded strings. With that in mind, let’s look at how to directly encode an NSData object to Base64 as well as how to decode an NSData object from Base64.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Create NSData object
NSData *dataTake2 =
  [@"iOS Developer Tips" dataUsingEncoding:NSUTF8StringEncoding];
 
// Convert to Base64 data
NSData *base64Data = [dataTake2 base64EncodedDataWithOptions:0];
NSLog(@"%@", [NSString stringWithUTF8String:[base64Data bytes]]);
 
// Do something with the data
// ...
 
// Now convert back from Base64
NSData *nsdataDecoded = [base64Data initWithBase64EncodedData:base64Data options:0];
NSString *str = [[NSString alloc] initWithData:nsdataDecoded encoding:NSUTF8StringEncoding];
NSLog(@"%@", str);

On line 2 we create our NSData test subject. Line 6 we call the base64EncodedDataWithOptions method of the NSData class to Base64 encode the data. I’ve included an NSString conversion of the data solely to verify what goes in is the same as what comes back out.

Decoding the Base64 NSData object is nothing more than calling the method initWithBase64EncodedData with the encoded NSData object (line 13). Once again, I’ve included an NSString conversion for testing.

Additional Reading

  • The NSData class reference includes the information on how to encode and decode NSData using Base64.
  • The NSString class reference explains the nuances of encoding and decoding when working with NSString objects.
  • I’ve found that encoding and decoding data from the terminal can be helpful when debugging. This Mac OS X doc describes the base64 OS X terminal utility.
时间: 2024-10-05 03:47:34

iOS 7: Base64 Encode and Decode NSData and NSString Objects的相关文章

java Base64 [ Encode And Decode In Base64 Using Java ]

http://www.javatips.net/blog/2011/08/how-to-encode-and-decode-in-base64-using-java http://commons.apache.org/proper/commons-codec/ 官方下载链接 Encode And Decode In Base64 Using Java explains about different techniques for Encode Base64 using java / Decode

(iOS)Base64加密和DES加密、以及JAVA和iOS中DES加密统一性问题

我们在项目中为了安全方面的考虑,通常情况下会选择一种加密方式对需要安全性的文本进行加密,而Base64加密和DES64加密是常用的加密算法.我记得我在前一个项目中使用的就是这两种加密算法的结合:Base64 + DES加密.当然这需要移动端和后台服务器做一个统一. 1.Base64加解密 值得一提的是:apple提供了基础的Base64加解密算法.这样我们就可以直接使用方法去实现Base64加解密.先看一下apple都提供了哪些方法: @interface NSData (NSDataBase6

Javascript Base64 Encode & Decode

html代码: 1 <!DOCTYPE html> 2 <html> 3 <head> 4 <title>Page Title</title> 5 <style type="text/css"> 6 *{font-family: Consolas;font-style: italic} 7 .responsebox{width:900px;margin:10px auto;padding:10px;border:2

node_nibbler:自定义Base32/base64 encode/decode库

https://github.com/mattrobenolt/node_nibbler 可以将本源码复制到自己需要的JS文件中,比如下面这个文件,一个基于BASE64加密请求参数的REST工具: [附件:]REST-TEST.html <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"

IOS下Base64加密

Base64加密是常用的加密算法,在IOS的Des加密算法中已经使用到了Base64算法,还是单独整理出来吧. Base64.h文件 // // Base64.h // Copyright (c) 2014年 grant. All rights reserved. // #import <Foundation/Foundation.h> @interface Based64 : NSObject //Base64加密 + (NSString *) encodeBase64WithString:

java URLEncoder 和Base64.encode()

参考: http://www.360doc.com/content/10/1103/12/1485725_66213001.shtml (URLEncode) http://blog.csdn.net/uikoo9/article/details/27981219 计算机中的数据都是二进制的,不管是字符串还是文件,而加密后的也是二进制的, 而我们要看到的往往是字符串,本文就介绍了将byte[]转为各种进制以及base64编码. 是一种编码方式,可以理解为复杂的进制,很多算法加密后输出的都是byt

python的str,unicode对象的encode和decode方法

python的str,unicode对象的encode和decode方法 python中的str对象其实就是"8-bit string" ,字节字符串,本质上类似java中的byte[]. 而python中的unicode对象应该才是等同于java中的String对象,或本质上是java的char[]. 对于 s="你好" u=u"你好" s="你好" u=u"你好" 1. s.decode方法和u.enc

Python之encode与decode浅析

 Python之encode与decode浅析 在 python 源代码文件中,如果你有用到非ASCII字符,则需要在文件头部进行字符编码的声明,声明如下: # code: UTF-8 因为python 只检查 #.coding 和编码字符串,为了美观等原因可以如下写法: #-*-coding:utf-8-*- 常见编码介绍: GB2312编码:适用于汉字处理.汉字通信等系统之间的信息交换. GBK编码:是汉字编码标准之一,是在 GB2312-80 标准基础上的内码扩展规范,使用了双字节编码.

iOS解决NSData转NSString后字符为空

iOS中,将NSData转NSString的一般方法为[[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];但是当data中包含00时,所获得的新字符就会为nil,这时我们应该这样转 [NSString stringWithUTF8String:[data bytes]];