Skip to content Skip to sidebar Skip to footer

Reading Unicode From Sqlite And Creating A Nsstring

Im working on an iOS app where I need to store and retrieve from an SQLite DB, a representation of a NSString that has subscripts. I can create a NSString at compile time with a co

Solution 1:

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

Solution 2:

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 {
  unsignedint    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];
}

Post a Comment for "Reading Unicode From Sqlite And Creating A Nsstring"