猿问

将HEX NSString转换为NSData

将HEX NSString转换为NSData

我正在尝试将十六进制转换NSString为NSData(我正在使用下面附加的代码)。以下是输出:


<00000000 00000000 00000000 00000000 00000000 00000000 00000000 00000000>

这看起来与我完全无关。关于哪里出错的任何想法/建议?


NSString *strData = @"72ff63cea198b3edba8f7e0c23acc345050187a0cde5a9872cbab091ab73e553";


NSLog(@"string Data length is %d",[strData length]);


NSMutableData *commandToSend= [[NSMutableData alloc] init];

unsigned char whole_byte;

char byte_chars[2];

int i;

for (i=0; i < [strData length]/2; i++) {


    byte_chars[0] = [strData characterAtIndex:i*2];

    byte_chars[1] = [strData characterAtIndex:i*2+1];

    whole_byte = strtol(byte_chars, NULL, [strData length]);

    [commandToSend appendBytes:&whole_byte length:1]; 

}

NSLog(@"%@", commandToSend);    


繁星点点滴滴
浏览 777回答 3
3回答

素胚勾勒不出你

NSString&nbsp;*command&nbsp;=&nbsp;@"72ff63cea198b3edba8f7e0c23acc345050187a0cde5a9872cbab091ab73e553";command&nbsp;=&nbsp;[command&nbsp;stringByReplacingOccurrencesOfString:@"&nbsp;"&nbsp;withString:@""];NSMutableData&nbsp;*commandToSend=&nbsp;[[NSMutableData&nbsp;alloc]&nbsp;init];unsigned&nbsp;char&nbsp;whole_byte;char&nbsp;byte_chars[3]&nbsp;=&nbsp;{'\0','\0','\0'};int&nbsp;i;for&nbsp;(i=0;&nbsp;i&nbsp;<&nbsp;[command&nbsp;length]/2;&nbsp;i++)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;byte_chars[0]&nbsp;=&nbsp;[command&nbsp;characterAtIndex:i*2]; &nbsp;&nbsp;&nbsp;&nbsp;byte_chars[1]&nbsp;=&nbsp;[command&nbsp;characterAtIndex:i*2+1]; &nbsp;&nbsp;&nbsp;&nbsp;whole_byte&nbsp;=&nbsp;strtol(byte_chars,&nbsp;NULL,&nbsp;16); &nbsp;&nbsp;&nbsp;&nbsp;[commandToSend&nbsp;appendBytes:&whole_byte&nbsp;length:1];&nbsp;}NSLog(@"%@",&nbsp;commandToSend);

神不在的星期二

这可能更有用,Apple已经共享了NSData类别。的NSData + HexString.m代码是:@implementation&nbsp;NSData&nbsp;(HexString)//&nbsp;Not&nbsp;efficent+(id)dataWithHexString:(NSString&nbsp;*)hex{ &nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;buf[3]; &nbsp;&nbsp;&nbsp;&nbsp;buf[2]&nbsp;=&nbsp;'\0'; &nbsp;&nbsp;&nbsp;&nbsp;NSAssert(0&nbsp;==&nbsp;[hex&nbsp;length]&nbsp;%&nbsp;2,&nbsp;@"Hex&nbsp;strings&nbsp;should&nbsp;have&nbsp;an&nbsp;even&nbsp;number&nbsp;of&nbsp;digits&nbsp;(%@)",&nbsp;hex); &nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;*bytes&nbsp;=&nbsp;malloc([hex&nbsp;length]/2); &nbsp;&nbsp;&nbsp;&nbsp;unsigned&nbsp;char&nbsp;*bp&nbsp;=&nbsp;bytes; &nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(CFIndex&nbsp;i&nbsp;=&nbsp;0;&nbsp;i&nbsp;<&nbsp;[hex&nbsp;length];&nbsp;i&nbsp;+=&nbsp;2)&nbsp;{ &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf[0]&nbsp;=&nbsp;[hex&nbsp;characterAtIndex:i]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;buf[1]&nbsp;=&nbsp;[hex&nbsp;characterAtIndex:i+1]; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char&nbsp;*b2&nbsp;=&nbsp;NULL; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;*bp++&nbsp;=&nbsp;strtol(buf,&nbsp;&b2,&nbsp;16); &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NSAssert(b2&nbsp;==&nbsp;buf&nbsp;+&nbsp;2,&nbsp;@"String&nbsp;should&nbsp;be&nbsp;all&nbsp;hex&nbsp;digits:&nbsp;%@&nbsp;(bad&nbsp;digit&nbsp;around&nbsp;%d)",&nbsp;hex,&nbsp;i); &nbsp;&nbsp;&nbsp;&nbsp;} &nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;[NSData&nbsp;dataWithBytesNoCopy:bytes&nbsp;length:[hex&nbsp;length]/2&nbsp;freeWhenDone:YES];}@end
随时随地看视频慕课网APP
我要回答