IOS Base64 Encoding (text/image)

Source: Internet
Author: User
Tags base64 encode

  Base64 is one of the most common encoding methods used to transmit 8bit bytes of code on the network, which can be converted to "string". In applications, it is often necessary to encode binary data into a form that is suitable for use in URLs, where Base64 encoding is not readable, i.e. the encoded data is not directly visible to the human eye.

   However, the standard Base64 is not intended to be transmitted directly in the URL because the URL encoder will change the '/' and ' + ' characters in the standard Base64 to '%XX ', and these '% ' numbers will need to be converted when they are stored in the database because ANSI SQL will '% ' is used as a wildcard character. To solve this problem, you can use an improved BASE64 encoding for URLs, which not only removes the padding ' = ' at the end, but also changes the "+" and "/" in standard Base64 to "-" and "_" respectively, thus eliminating the need for conversion in URL codec and database storage. Avoids the increase in the length of encoded information in this process, and unifies the format of object identifiers in databases, forms, and so on. There is also an improved BASE64 variant for regular expressions that changes "+" and "/" to "!" and "-", because "+", "*" and the "[" and "]" used in the preceding IRCU may have special meanings in regular expressions.

    BASE64 encoding is essentially a scheme for turning binary data into textual data.

 For non-binary data, it is first converted to binary form, and then each successive 6 bits (2 of the 6 square =64) calculates its decimal value, according to the value in the above index table to find the corresponding character, and finally get a text string. The Base64 Index table for the code "man"    standard is as follows, and another auxiliary character ' = ' is used as a suffix

    if the number of bytes to encode cannot be divisible by 3, the last 1 or 2 bytes will be Then you can use the following method to process: First use the 0-byte value at the end of the top, so that it can be divisible by 3, and then the Base64 encoding. After the encoded base64 text, add one or two ' = ' sign, which represents the number of bytes to be replenished. That is, when the last remaining eight-bit byte (a byte), the last 6-bit base64 byte block has four bits is 0 value, and the last two equals, if the last two bits of eight bytes (2 byte), the last 6 bits of the base byte block has two bits is the 0 value, and finally append an equal sign. Refer to the following table:   "Thunder Download" as an example: many download-type websites are provided "Thunder download" link, its address is usually encrypted thunderbolt dedicated. 

In fact, Thunderbolt's "dedicated address" is also used Base64 "Encryption", the process is as follows: 

1, before and after the address of the respective addAA and ZZ 

2, the new string is BASE64 encoded 

In addition:flashget  similar to thunder, but in the first step when adding the "material" is different, Flashget in the address before and after the "material" is [Flashget] and QQ Whirlwind simply do not feed, Base64 encode the address directly   method:


- (NSString *)base64EncodedString {
    NSData *data = [self dataUsingEncoding:NSUTF8StringEncoding];
    return [data base64EncodedStringWithOptions:0];
}

- (NSString *)base64DecodedString {
    NSData *data = [[NSData alloc]initWithBase64EncodedString:self options:0];
    return [[NSString alloc]initWithData:data encoding:NSUTF8StringEncoding];
}



BASE64 Encoding of characters:

NSString *sourceStr = @"www.baidu.com";
NSLog(@"sourceStr---->%@",sourceStr);

NSString *base64Str = [sourceStr base64EncodedString];
NSLog(@"Base64 encoding---->%@",base64Str);

NSString *decodeStr = [base64Str base64DecodedString];
NSLog (@"Base64 decoding---->%@",decodeStr);

/*
  sourceStr---->www.baidu.com
  Base64 encoding---->d3d3LmJhaWR1LmNvbQ==
  Base64 decoding---->www.baidu.com
  */



Base64 Codecs for images:

// The UIImage image is converted to a Base64 string:
UIImage *img = [UIImage imageNamed:@"0.jpg"];
NSData *imgData = UIImageJPEGRepresentation(img, 1.0f);
NSString *encodedImgStr = [imgData base64EncodedStringWithOptions:NSDataBase64Encoding64CharacterLineLength];

NSLog(@"encodedImgStr---->%@",encodedImgStr);

//Base64 string to UIImage image:
NSData *decodedImgData = [[NSData alloc] initWithBase64EncodedString:encodedImgStr options:NSDataBase64DecodingIgnoreUnknownCharacters];
UIImage *decodedImage = [UIImage imageWithData:decodedImgData];

UIImageView *imgV = [[UIImageView alloc] initWithFrame:CGRectMake(0, 100, [UIScreen mainScreen].bounds.size.width, [UIScreen mainScreen].bounds.size.width)];
imgV.contentMode = UIViewContentModeScaleAspectFit;
[imgV setImage:decodedImage];
[self.view addSubview:imgV];

NSLog(@"decodedImage---->%@",decodedImgData);



IOS Base64 Encoding (text/image)


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.