iOS Base64 codec tool class and use

Source: Internet
Author: User

In order to avoid transmitting HTTP content in plaintext, it can be transmitted by Base64 encoding, then decoded by the receiver, and also facilitates the string transmission of 2 binary data.

For iOS, Google offers a great tool class for Base64 codecs, and of course, it can be done with OpenSSL, but it's relative.

Say more trouble. Google has provided 3 of files is enough.

The official website address is:

http://code.google.com/p/google-toolbox-for-mac/

There are a lot of baby, if you find it hard to find these 3 files, so I added to the attachment. Unzip it and put it in the iOS project.

Introduction to How to use:

See the GTMBase64.h header file with a detailed description of the function, here are just a few two examples:

1. Encoding, i.e. Base64 encryption:

Using functions:

Decodedata:

//

Base64 decodes contents of the NSData object.

//

Returns:

A new autoreleased NSData with the decoded payload. Nil for any error.

//

+ (NSData *) Decodedata: (nsdata *) data;

Examples of Use methods:

NSString *input =@ "Hello World";

NSData *data = [input datausingencoding: nsutf8stringencoding allowlossyconversion:YES];

data = [GTMBase64 encodedata:data];

nsstring *base64string = [[nsstring alloc] initwithdata:d ATA encoding: Nsutf8stringencoding];

NSLog (@ "base64string =%@", base64string);

If successful, the encrypted data is printed:sgvsbg8sifdvcmxk

This will allow you to transfer from HTTP.

2. Decoding, i.e. Base64 decryption:

Here you use the function:

Decodestring:

//

Base64 decodes contents of the NSString.

//

Returns:

A new autoreleased NSData with the decoded payload. Nil for any error.

//

+ (NSData *) decodestring: (NSString *) string;

Use this method:

nsdata *data =[GTMBase64 decodestring:@ "sgvsbg8sifdvcmxk"];

In this way, the original text of the NSString type is parsed into the NSData type of data, which can be used

NSLog(@ "data =%@", data);

Print out, log output 16 binary for example:

BASE64TEST[5670:C07] data = <01000a6e 735f766f 645f3030 3102005c 81705900 00000000 005c8020 0101124c 803a0001 18beee29 11aef543 7a02e35c 012769AD 08619971 50e57f2c>

If the data is output with the NSString type, the content is Hello, World

iOS Base64 codec tool class and use

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.