I have an NSData instance containing <7e0006b5 01012605 06070172 7a>, how can I convert it to an NSString containing @”7e0006b501012605060701727a”?
My test code is:
char c_command[26] = {'7', 'e', '0', '0',
'0', '6', 'b', '5',
'0', '1', '0', '1',
'2', '6', '0', '5',
'0', '6', '0', '7',
'0', '1', '7', '2',
'7', 'a'};
NSMutableData *commandToSend = [[NSMutableData alloc] init];
for (int i = 0; i < 13; i++) {
unsigned char data_byte;
char byte_chars[3] = {'\0','\0','\0'};
byte_chars[0] = c_command[i * 2];
byte_chars[1] = c_command[i * 2 + 1];
data_byte = strtol(byte_chars, NULL, 16);
[commandToSend appendBytes:&data_byte length:1];
}
NSString *str = [[NSString alloc] initWithData:commandToSend encoding:NSASCIIStringEncoding];
NSLog(@"Test command is :%@ \n command to string is:%@", commandToSend, str);
the output of log is:
Test command is :<7e0006b5 01012605 06070172 7a> command to string is:~
1 Answer