Convert nsdata to nsstring with no parentheses or spaces

Source: Internet
Author: User

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:

 

 

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.