iOS file type judgment

Source: Internet
Author: User

Recently doing something that has download zip, just the server sent over is binary, need to according to binary to determine what type of file, thereby saving operations. At first it was not understood, and it was later discovered that the first 2 bits of the ASCII code could be judged by binary binary. As follows:

255216 jpg;
7173 gif;
6677 BMP,
13780 PNG;
6787 SWF
7790 exe DLL,
8297 rar
8075 zip
55122 7z
6063 XML
6033 html
239187 aspx
117115 CS
119105 JS
102100 txt
255254 SQL

In iOS, use the OC code to do the following:

NSString *path = [[NSBundle mainbundle] Pathforresource:@"Test"OfType:@"Zip"]; NSData*data =[NSData Datawithcontentsoffile:path];if(data.length<2) {        return ; }intCHAR1 =0, char2 =0;//This must be initialized[Data getBytes:&char1 Range:nsmakerange (0,1)]; [Data getBytes:&char2 Range:nsmakerange (1,1)]; NSLog (@"%d%d", CHAR1,CHAR2); NSString*NUMSTR = [NSString stringWithFormat:@"%i%i", CHAR1,CHAR2]; NSLog (@"%@", NUMSTR);

This way you can see that the output is: 8075.

It's just a zip with the numbers above.

iOS file type judgment

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.