reading unicode from sqlite and creating a NSString

谁说胖子不能爱 提交于 2019-12-01 13:46:22

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

I solved the problem like this - error checking removed for clarity -

the unicode string comes into the parameter stringEncoded from the db like:

"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];
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!