One, the conversion between each other
1. String to data stream NSData
@ "abc123"; *DD = [str datausingencoding:nsutf8stringencoding]; NSLog (@ "%@"<61626331 3233>
By printing the converted data stream you can see that the data stream is a two-bit byte that stores the hexadecimal character of the string ascii, a = = 0x61, 1 = $ = 0x31
2, byte and data stream conversion
Byte bb[] = {98 , 99 , 49 , 50 , " ; NSData *dd = [[NSData alloc]initwithbytes:bb length:6
3, according to the need to directly into the nsdata, upside down the same nsdata to the string is also the first byte in the turn string
NSString *bcdstr =@"616263313233"; intLeng = (int) bcdstr.length/2; if(bcdstr.length%2==1)//judging Odd even numbers{Leng+=1; } Byte Bbte[leng]; for(inti =0; i<leng-1; i++) {Bbte[i]= (int) Strtoul ([[Bcdstr Substringwithrange:nsmakerange (i*2,2)]utf8string],0, -); } if(bcdstr.length%2==1) {Bbte[leng-1] = (int) Strtoul ([[Bcdstr Substringwithrange:nsmakerange (Leng-1)*2,1)]utf8string],0, -) * -; }Else{Bbte[leng-1] = (int) Strtoul ([[Bcdstr Substringwithrange:nsmakerange (Leng-1)*2,2)]utf8string],0, -); } NSData*de =[[NSData alloc]initwithbytes:bbte Length:leng]; NSLog (@"%@", DE);//<61626331 3233>
Second, MD5 calculation
1. MD5 Calculation of String
+ (NSString *) Md5hexdigest: (NSString *) str{Const Char*original_str =[str utf8string]; unsignedCharResult[cc_md5_digest_length]; CC_MD5 (Original_str, (int) strlen (ORIGINAL_STR), result); Nsmutablestring*hash = [nsmutablestringstring]; for(inti =0; I < -; i++) {[Hash AppendFormat:@"%02x", Result[i]];//two-bit means less than two-bit complement 0 } return[hash lowercasestring];}
2. MD5 Calculation of String
+ (nsstring*) Getmd5withdata: (NSData *) data{//do not turn the stream into char first, or if you encounter 00 bytes, you will not calculate 00 after the dataUnsignedCharresult[ -]; Cc_md5 ([Data bytes], (int) [Data length], result); Nsmutablestring*hash = [nsmutablestringstring]; for(inti =0; I < -; i++) {[Hash AppendFormat:@"%02x", Result[i]];//x is lowercase letters, x is uppercase letters, 2 bytes is less than 0 } returnHash;}
IOS BCD, data Flow, byte, and MD5 calculations