-1

Hey I want to know how to convert a printed data (i printed it with NSLog) such as this:

<01000405 00000000 00000000 00000000 00000000>

Back to NSData?

I tried

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

But it just gave me this result:

<3c303130 30303430 32203030 30303030 30302030 30303030 30303020 30303030 30303030 20303030 30303030 303e>
Gili Ariel
  • 572
  • 1
  • 6
  • 18

1 Answers1

0
NSString *command = @"72ff63cea198b3edba8f7e0c23acc345050187a0cde5a9872cbab091ab73e553";

command = [command 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]; 
}
NSLog(@"%@", commandToSend);

from here : Converting HEX NSString To NSData

To replace "<" , " " , ">" use this method:

NSString * newString = [myString stringByReplacingOccurrencesOfString:@"<" withString:@""];
Community
  • 1
  • 1
Vyacheslav
  • 26,359
  • 19
  • 112
  • 194