从sqlite读取unicode并创建NSString [英] reading unicode from sqlite and creating a NSString

查看:98
本文介绍了从sqlite读取unicode并创建NSString的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在iOS应用程序上工作,我需要从SQLite DB存储和检索,这是一个具有下标的NSString的表示。我可以在编译时使用常量创建一个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 constant:

@Br\\\₂_CCl\\\₄

@"Br\u2082_CCl\u2084"

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

\u2082 is a 2 subscript, \u2084 is a 4 subscript. What im storing in the SQLite db is:

Br\\\₂_CCl \ u2084

"Br\u2082_CCl\u2084"

但是我无法弄清楚该怎么做,将其重新转换回NSString。数据从数据库返回作为char *Br \\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\我需要一种方法让它回到NSString - 谢谢!

But what I can not figure out how to do, is reconvert that back into an NSString. The data comes back from the db as a char * "Br\\u2082_CCl\\u2084" Stripping out the extra slash has made no difference in my feeble experiments. I need a way get that back into an NSString - Thanks!

推荐答案

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

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

unicode字符串进入参数stringEncoded from db like:

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

MgBr \ u2082_CH \\ \₂Cl\\\₂\"

"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];
}

这篇关于从sqlite读取unicode并创建NSString的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆