0

我正在开发一个 iOS 应用程序,我需要在其中存储和检索 SQLite DB,这是具有下标的 NSString 的表示。我可以在编译时使用常量创建一个 NSString:

@"Br\u2082_CCl\u2084"

\u2082 是 2 下标,\u2084 是 4 下标。我存储在 SQLite 数据库中的是:

“Br\u2082_CCl\u2084”

但是我不知道该怎么做,就是将其重新转换回 NSString。数据以 char * "Br\\u2082_CCl\\u2084" 的形式从数据库中返回,去掉多余的斜线对我微弱的实验没有任何影响。我需要一种方法将其恢复为 NSString - 谢谢!

4

2 回答 2

0

You need one of the NSString stringWithCString class methods or the corresponding initWithCString instance methods.

于 2011-01-12T00:59:00.277 回答
0

我解决了这样的问题-为清楚起见删除了错误检查-

unicode 字符串从 db 进入参数 stringEncoded,如:

"MgBr\u2082_CH\u2082Cl\u2082"

+(NSString *)decodeUnicodeBytes:(char *)stringEncoded {
  unsigned int    unicodeValue;
  char            *p, buff[5];
  NSMutableString *theString;
  NSString        *hexString;
  NSScanner       *pScanner;

  theString = [[[NSMutableString alloc] init] autorelease];
  p = stringEncoded;

  buff[4] = 0x00;
  while (*p != 0x00) {

    if (*p == '\\') {
      p++;
      if (*p == 'u') {
        memmove(buff, ++p, 4);

        hexString = [NSString stringWithUTF8String:buff];
        pScanner = [NSScanner scannerWithString: hexString];
        [pScanner scanHexInt: &unicodeValue];

        [theString appendFormat:@"%C", unicodeValue];
        p += 4;
        continue;
        }
      }

    [theString appendFormat:@"%c", *p];
    p++;
    }

  return [NSString stringWithString:theString];
}
于 2011-01-19T19:44:08.783 回答