-2

I have Receiving NSData like 210502113c4d from the server . I am getting this data into NSString format . I want to retrieve the 3 byte from this incoming data i.e. 2105 02 113c4d. I have tried following :

NSData *demoData=[string dataUsingEncoding:NSASCIIStringEncoding];
NSLog(@"demo data :%@",demoData);
const char *bytes=[data bytes];
NSMutableString *result = [NSMutableString string];
for (int i = 0; i < [demoData length]; i++)
{
    [result appendFormat:@"%02hhx", (unsigned char)bytes[i]];
}

But its not working . Please suggest me some solution to achieve this ?

4

1 回答 1

0

代码获取字符串第三个字节的值,假设字符串包含十六进制字节,hh格式:

NSString* string = @"210502113c4d";
NSString* thirdByte = [string substringWithRange:NSRangeFromString(@"4,2")];
NSScanner* scanner = [NSScanner scannerWithString:thirdByte];
NSUInteger value;
[scanner scanHexInt:&value];
NSLog(@"%u", value); // 2
于 2013-04-08T10:18:38.067 回答