将NSData替换为特定的NSStrting格式

时间:2012-08-08 18:13:34

标签: iphone nsstring

我收到的NSData包含Hexa值,如下所示

   <aaaaaaa aaaaaab>

我只是通过将NSData转换为NSString来获得此功能,现在我需要以下列格式显示上面的字符串

AA:AA:AA:AA  AA:AA:AA:AB

请帮助我如何获得以上格式,请帮助

2 个答案:

答案 0 :(得分:1)

您可以尝试:

str = @"<aaaaaaa aaaaaab>";
str = [str stringByReplacingOccurrencesOfString:@"<" withString:@""]; //Remove <
str = [str stringByReplacingOccurrencesOfString:@" " withString:@""]; //Remove space
str = [str stringByReplacingOccurrencesOfString:@">" withString:@""]; //Remove >
NSMutableString *mStr = [[NSMutableString alloc] initWithString:str];

for (int i=2; i< [mStr length]; i+=3) {
    [mStr insertString:@":" atIndex:i];
}
for (int i=11; i< [mStr length]; i+=12) {
    [mStr insertString:@" " atIndex:i];
}

这将删除空格,&lt;和&gt ;.然后加入:和空格。

答案 1 :(得分:0)

str = [str stringByReplacingOccurrencesOfString:@":"
                                     withString:@""];

str = [str lowercaseString];