IOS BCD, data Flow, byte, and MD5 calculations

Source: Internet
Author: User

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

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.