"Go" ios in 16 binary to 10 binary

Source: Internet
Author: User

Original URL: http://www.voidcn.com/blog/u012198553/article/p-4976772.html

Converts a hexadecimal string to NSData
-(NSData) Transstrhextodata: (nsstring) Strhex
{
Bytes Index
Nsuinteger j = 0;

Nsinteger len = Strhex. length;if (len%2 = =1) {If it is not divisible by 2, then the 16-binary string does not satisfy the picture graph. It is hereby stated that if you simply convert the 16 into NSData, you can kill the IF,ReturnNil;}Dynamically allocating memory Byte *bytes = (byte *) malloc (len/2 +1) *sizeof (Byte));Initialize memory where memset is the function of populating a block of memory with a given value, which is the quickest way to clear 0 operations on large structures or arrays memset (bytes,' + ', (Len/2 +1) *sizeof (Byte));The For loop is actually the process of converting a 16 binary string into a byte arrayfor (Nsuinteger i =0; I < Strhex.length; i + = 2) {///a byte of byte is 8 bits ( bit) bit one is represented by a 0 or 1 (that is, binary) every 8 bits (bit) of a byte (byte) so every time take 2 for a character combination into a byte is actually 2 16 binary characters is actually 8 bits (bit) that is a byte nsstring *str = [Strhex substringwithrange:nsmakerange (i, 2)]; ///convert 16 binary string to decimal unsigned long uint_ch = Strtoul ([str utf8string], 0, 16); bytes[j] = uint_ch; ///self-increment j + +;} ///convert byte array to Nsdatansdata *data = [[ nsdata alloc] initwithbytes:bytes length:len/2];///release Memory Free (bytes); return data;            

}

"Go" ios in 16 binary to 10 binary

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.