如何再次将打印的 NSData 转换为 NSData?

How to convert printed NSData to NSData again?

嘿,我想知道如何转换打印数据(我用 NSLog 打印它),例如:

<01000405 00000000 00000000 00000000 00000000>

返回NSData?

我试过了

[NSString stringWithFormat:@"<01000405 00000000 00000000 00000000 00000000>"] dataUsingEncoding:NSUTF8StringEncoding]

但它只给了我这个结果:

<3c303130 30303430 32203030 30303030 30302030 30303030 30303020 30303030 30303030 20303030 30303030 303e>
NSString *command = @"72ff63cea198b3edba8f7e0c23acc345050187a0cde5a9872cbab091ab73e553";

command = [command stringByReplacingOccurrencesOfString:@" " withString:@""];
NSMutableData *commandToSend= [[NSMutableData alloc] init];
unsigned char whole_byte;
char byte_chars[3] = {'[=10=]','[=10=]','[=10=]'};
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]; 
}
NSLog(@"%@", commandToSend);

从这里开始:Converting HEX NSString To NSData

要替换“<”、“”、“>”,请使用此方法:

NSString * newString = [myString stringByReplacingOccurrencesOfString:@"<" withString:@""];