简体   繁体   English

使用NSOutputStream发送字节数组

[英]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.

相关问题 使用NSOutputStream通过套接字发送到服务器时,用换行符分隔数据(JSON) - Delimit data (JSON) with newline when sending to server over socket using NSOutputStream EASession的NSOutputStream停止将数据发送到EAAccessory - NSOutputStream of EASession stops sending data to EAAccessory 使用NSOutputStream发送推送通知返回-9844 - Sending push notifications with NSOutputStream returns -9844 使用NSOutputstream有什么优势? - What's the advantages of using NSOutputstream? 如何使用NSOutputStream通过套接字发送NSString - How to send NSString through socket using NSOutputStream 编码来自AudioUnit的录音以在iO中通过NSOutputStream发送 - Encoding recording from AudioUnit for sending over NSOutputStream in iOs 通过字节数组将图像从iOS发送到.net服务器 - Sending an image via byte array from iOS to a .net server 如何使用NSInputStream和NSOutputStream读取和写入音频文件 - How to read and write audio file using NSInputStream and NSOutputStream 错误(“'()'与'UInt8'不相同”)使用Swift中的write函数将NSData字节写入NSOutputStream - Error (“'()' is not identical to 'UInt8'”) writing NSData bytes to NSOutputStream using the write function in Swift iOS:当我尝试发送消息时,无法使用NSOutputStream连接将消息发送到套接字服务器 - iOS:Unable to send message to socket server using NSOutputStream connection drops when i try to send message
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM