Hi I am trying to create a packet that I want to send over the network to a server, I have pretty much got the packet sorted however its missing a length identifier which I need to calculate at the end of my method and add into the packet.
The packet structure is like this
- leading value identifier (UInt16)
- content size (UInt32)
- packet Content (string)
currently my method looks something like this
- (NSMutableData *) addRegCode
{
//Supply some default string for testing
NSString *regCode = [[NSString alloc] initWithString:@"abcd1"];
//create NSData object
NSData *registrationCodeData = [regCode dataUsingEncoding:NSUTF8StringEncoding];
NSMutableData * RegistrationCodeMutableData = [[NSMutableData alloc] init]; //send this object
//create leading value
UInt16 leadingValue = 8;
NSData * leadingValueData = [[NSData alloc] initWithBytes:&leadingValue length:sizeof(leadingValue)];
//append data to mutableData
[RegistrationCodeMutableData appendData:leadingValueData];
[RegistrationCodeMutableData appendData:registrationCodeData];
return RegistrationCodeMutableData;
}
what I would like to know is how to calculate the size of registrationCodeData and then add it between where I append leadingValueData & registrationCodeData
I think I have to be using dataWithBytes:length: but I’m not 100% sure of how to use this
this should do it.
also of note using capital letters to start variable names is a little atypical and can be confusing to read!