如何将十六进制字符串转换为十六进制数组?

时间:2012-02-07 05:21:46

标签: objective-c ios hex

例如:
我有一个字符串:hexInString = @"0123456789abcdef",我想将它转换为十六进制数组,如:

{01, 23, 45, 67, 89, ab, cd, ef}

或:

{0123, 4567, 89ab, cdef}

我尝试这样做,但因十进制数组而失败:

+ (NSMutableArray *)generateHexArrayFrom:(NSString *)hexInString
{
  NSMutableArray * hexArray = [[NSMutableArray alloc] init];
  int hexStringSize = sizeof(uint_fast32_t) / 2;

  for (int x = 0; x <= [hexInString length] - hexStringSize; x += hexStringSize) {
    NSScanner * scanner = [[NSScanner alloc] initWithString:[hexInString substringWithRange:NSMakeRange(x, hexStringSize)]];
    uint_fast32_t hex;
    [scanner scanHexInt:&hex];
    [hexArray addObject:[NSNumber numberWithInt:hex]];
  }

  return [hexArray autorelease];
}

如何向NSMutableArray添加十六进制值?或者是否有存储数据的类型?

0 个答案:

没有答案