Object-C hex operation

Source: Internet
Author: User
# Import <Foundation/Foundation. h> @ interface nsstring (froad_nsstring_additionss)-(byte *) getbytes; + (nsstring *) stringfrombytes :( byte *) byte byteslen :( INT) Len; @ end @ implementation nsstring (bytes) -(byte *) getbytes {nsdata * Data = [self datausingencoding: nsutf16bigendianstringencoding]; byte * v_bytes = (byte *) [data bytes]; return v_bytes; // nsmutablearray * array = [[NSM Utablearray alloc] init] autorelease]; // For (INT I = 0; I <[Data Length]; I ++) // {// [array addobject: [nsnumber numberwithchar: v_bytes [I]; //} // return [Data Length];} + (nsstring *) stringfrombytes :( byte *) byte byteslen :( INT) len {nsdata * Data = [[nsdata alloc] initwithbytes: Byte Length: Len]; nsstring * s = [[[nsstring alloc] initwithdata: data encoding: nsutf16bigendianstringencoding] oraut ]; Return s ;}@ end @ interface HEX: nsobject {}@ end @ implementation hex + (byte) numfromchar: (char) c {If (C> = '0' & C <= '9') {return C-'0 ';} else if (C> = 'A' & C <= 'Z') {return C-'A' + 10 ;} else if (C> = 'A' & C <= 'Z') {return C-'A' + 10;} return-1 ;}+ (nsdata *) datafromhexstring: (nsstring *) S_T {// 4f60 597d // 0100 1111s_t = [S_T stringbyreplacingoccurrencesofstring: @ "" withstring: @ ""]; If ([S_T length] % 2! = 0) {return nil;} byte * retbytes = malloc (sizeof (char) * [S_T length]); byte * Ori = retbytes; For (INT I = 0; I <[S_T length];) {char highbit = [S_T characteratindex: I ++]; char lowbit = [S_T characteratindex: I ++]; // to byte A = [hex numfromchar: highbit]; byte B = [hex numfromchar: lowbit]; * (retbytes ++) = (a <4) | B;} nsdata * Data = [nsdata datawithbytes: Ori length: [S_T length]/2]; return data ;}+ (nsstring *) stringfrombyte :( byte) byteval; {nsmutablestring * STR = [nsmutablestring string]; byte byte1 = byteval> 4; byte byte2 = byteval & 0xf; [STR appendformat: @ "% x", byte1]; [STR appendformat: @ "% x", byte2]; return STR;} // 0000 0000 0000 0000 + (nsstring *) stringfromshort :( short) shortval {nsmutablestring * STR = [nsmutablestring string]; [STR appendformat: @ "% x", shortval> 12]; [STR appendformat: @ "% x ", (shortval> 8) & 0xf]; [STR appendformat: @ "% x", (shortval> 4) & 0xf]; [STR appendformat: @ "% x ", shortval & 0xf]; return STR ;}+ (nsstring *) hexstringfromdata :( nsdata *) Data {nsmutablestring * STR = [nsmutablestring string]; byte * byte = (byte *) [data bytes]; for (INT I = 0; I <[Data Length]; I ++) {[STR appendstring: [hex stringfrombyte: * (byte + I)];} return STR ;}@ endint main (INT argc, const char * argv []) {NSAID utoreleasepool * Pool = [[NSAID utoreleasepool alloc] init]; // string to UTF bytes. // nsstring * Ori = @ "registered successfully 123"; // nsdata * s_data = [Ori datausingencoding: nsutf16bigendianstringencoding]; // nslog (@ "% @", s_data); // UTF bytes to string. /// nsstring * s = [[nsstring alloc] initwithdata: s_data // encoding: nsutf16bigendianstringencoding]; // nslog (@ "% @", S ); // hexnsstring * s = [hex stringfromshort :( short) 0x123a]; nslog (@ "% @", S); nsstring * v_s = @ "4f60 597d "; nsdata * Data = [hex datafromhexstring: v_s]; nslog (@ "% @", data); nslog (@ "% @", [hex hexstringfromdata: Data]); [pool drain]; return 0 ;}

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.