In my code I construct a hex NSString first and then use the utility function below to convert it to NSData for transmission. For example:
+ (NSData *)convertHexString:(NSString *)hexString {
NSString *command = [hexString stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData *commandToSend = [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = { '\0', '\0', '\0' };
int i;
for (i = 0; i < [command length] / 2; i++) {
byte_chars[0] = [command characterAtIndex:i * 2];
byte_chars[1] = [command characterAtIndex:i * 2 + 1];
whole_byte = strtol(byte_chars, NULL, 16);
[commandToSend appendBytes:&whole_byte length:1];
}
return commandToSend;
}
Now there is a requirement that specifies "NSData must be a minimum of 8 bytes and be a multiple of 8 bytes. NULL padding can be used to make the data a multiple length of 8 bytes." I am not sure how I can make this happen.
NSString* hexString = @"FF88";//this is two bytes right now.
//how do I add NULL padding so that it becomes 8 bytes?
Thank you!