Objective c 目标c,相当于c中的CopyTo#

Objective c 目标c,相当于c中的CopyTo#,objective-c,Objective C,我有一个C代码,它从特定索引将字节复制到字节数组,如下例所示: string headerInfo = String.Format(source + "<>" + destination + "<>" + sessionId); headerInfo = headerInfo.TrimEnd('\n', '\0', '\r'); byte[] headerInfoBytes = Encoding.UTF8.GetBytes(headerInfo); byte[] head

我有一个C代码,它从特定索引将字节复制到字节数组,如下例所示:

string headerInfo = String.Format(source + "<>" + destination + "<>" + sessionId);
headerInfo = headerInfo.TrimEnd('\n', '\0', '\r');
byte[] headerInfoBytes = Encoding.UTF8.GetBytes(headerInfo);
byte[] headerInfoLength = BitConverter.GetBytes(headerInfo.Length);

//create an byte Array with proper size.
byte[] sendData = new byte[4 + 4 + headerInfoBytes.Length + dataContractBytes.Length];

headerInfoLength.CopyTo(sendData, 0);
dataContractLengthBytes.CopyTo(sendData, 4);
headerInfoBytes.CopyTo(sendData, 8);
dataContractBytes.CopyTo(sendData, 8 + headerInfoBytes.Length);
m_clientSocket.Send(sendData);
string headerInfo=string.Format(源+“”+目标+“”+会话ID);
headerInfo=headerInfo.TrimEnd('\n','\0','\r');
byte[]headerInfoBytes=Encoding.UTF8.GetBytes(headerInfo);
byte[]headerInfoLength=位转换器.GetBytes(headerInfo.Length);
//创建大小合适的字节数组。
byte[]sendData=新字节[4+4+headerInfoBytes.Length+dataContractBytes.Length];
headerInfoLength.CopyTo(sendData,0);
dataContractLengthBytes.CopyTo(sendData,4);
headerInfoBytes.CopyTo(sendData,8);
dataContractBytes.CopyTo(sendData,8+headerInfoBytes.Length);
m_clientSocket.Send(sendData);
我的问题是,我怎样才能用objective c方式实现CopyTo?

查看如何处理可变数据

如果您特别需要索引部分,可以使用
replaceBytesInRange:withBytes:
。否则,只需附加数据:

NSString *headerInfo = [NSString stringWithFormat:@"%@<>%@<>%@", source, destination, sessionId];
NSData *headerData = [headerInfo dataUsingEncoding:NSUTF8StringEncoding];

NSMutableData *data = [NSMutableData dataWithCapacity:headerData.length + ...];
[data appendData:headerData];
NSString*headerInfo=[NSString stringWithFormat:@“%@%@”,源、目标、会话ID];
NSData*headerData=[headerInfo dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData*数据=[NSMutableData数据带容量:headerData.length+…];
[数据附件数据:headerData];