IOS byte-byte arrays related operations

Source: Internet
Author: User


1 intercept byte[] Part of the data, starting from begin, length is count


-(void) Bytesplit2byte: (byte[]) src orc: (byte[]) ORC begin: (Nsinteger) Begin count: (Nsinteger) count{

memset (orc, 0, sizeof (char) *count);

for (Nsinteger i = begin; I < Begin+count; i++) {

Orc[i-begin] = Src[i];

}

}


2 The byte array is converted to data of type int, which provides a storage idea


-(int) Lbytestoint: (byte[]) Byte

{

int height = 0;

NSData * TestData =[nsdata datawithbytes:byte length:4];

for (int i = 0; i < [testData length]; i++)

{

if (byte[[testdata Length]-i] >= 0)

{

Height = height + byte[[testdata length]-i];

} else

{

Height = height + 256 + byte[[testdata length]-i];

}

Height = height * 256;

}

if (Byte[0] >= 0)

{

Height = height + byte[0];

} else {

Height = height + 256 + byte[0];

}

return height;

}


3 turn int into byte[]


int jsonlen = Jsondata.length;

Char *p_json = (char *) &jsonlen;

Char Str_json[4] = {0};

for (int i= 0; i < 4; i++)

{

Str_json[i] = *p_json;

P_json + +;

}

Specific use of local, for example, custom socket protocol. The data size needs to be passed in, and the length of the int needs to be converted to byte[] so that it can be placed in the byte of the corresponding protocol




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.