Nsstring * strtest = @ "hello ";
Nsdata * datastring = [strtest datausingencoding: nsutf8stringencoding];
If nslog (@ "% @", datastring );
You will see the following in the console:
To remove the angle brackets and spaces at the beginning and end, we can add a category to nsdata.
// Nsdatatonsstring. h
@ Interface nsdata (tonsstring)
-(Nsstring *) converttonsstring;
@ End
// Nsdatatonsstring. m
@ Implementation nsdata (tonsstring)
-(Nsstring *) converttonsstring {
Nsmutablestring * strtemp = [nsmutablestring stringwithcapacity: [self length] * 2];
Const unsigned char * szbuffer = [self bytes];
For (nsinteger I = 0; I <[self length]; ++ I ){
[Strtemp appendformat: @ "% 02x", (unsigned long) szbuffer [I];
}
Return [[strtemp retain] autorelease];
}
@ End
The project contains the above two files,
If nslog (@ "data string: % @", [datastring converttonsstring]); you will see: