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