iosiphonensoutputstream

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 .

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

Any Help will be appreciated


Solution

  • 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.

    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