[英]Sending Byte Array using NSOutputStream
I have To Send 8 Byte Array To IP I have my data as NSMutableArray Contains integer values between 0 and 255 And as far as I know I have to convert it to nsdata before sending it . 我必须将8字节数组发送到IP,我的数据为NSMutableArray,其中包含0到255之间的整数值。据我所知,在发送之前必须将其转换为nsdata。
NSString *error;
NSData *data = [NSPropertyListSerialization dataFromPropertyList dataTobeSent format:NSPropertyListBinaryFormat_v1_0 errorDescription:&error];
[outputStream write:[data bytes] maxLength:[data length]];
I am using this way but it gives me NSdata object with more than 8 bytes 我正在使用这种方式,但是它给了我超过8个字节的NSdata对象
Any Help will be appreciated 任何帮助将不胜感激
You have made a wrong assumption about NSPropertyListSerialization, whatever the format you specify, it will construct a NSData object that can be transformed back into a propertyListObject, so you will have far more than just raw data. 您对NSPropertyListSerialization做出了错误的假设,无论您指定哪种格式,它都会构造一个NSData对象,该对象可以转换回propertyList对象,因此您不仅拥有原始数据。
You should made something like 你应该做类似
uint8_t dataArray[8]; // an 8 byte array
for (NSInteger i = 0; i < 8; i++) {
dataArray[i] = (uint8_t) [dataTobeSent[0] integerValue];
}
[outputStream write:dataArray maxLength:8];
be sure your dataTobeSent really is an array with exactly 8 values, else adapt the code 确保您的dataTobeSent实际上是一个具有8个值的数组,否则请修改代码
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.