如何将unicode转换为字符串?

时间:2014-01-29 09:24:30

标签: ios iphone ios5 unicode

我必须将unicode char转换为NSString 我要转换的文字是doc.title ="你好64 \ U00bf 世界样本" 我的代码是:

UILabel *lblName=[[UILabel alloc]initWithFrame:CGRectMake(15, 0, 250, 88)];
            const char *cString = [doc.title UTF8String];//where doc.title is above text
            NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
            NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            lblName.text= string;
            NSLog(@"Title is = %@ \n",doc.title);
            lblName.font=[UIFont boldSystemFontOfSize:16];
            lblName.numberOfLines=100;
            lblName.backgroundColor=[UIColor clearColor];
            lblName.textColor=[UIColor  colorWithRed:2.0/255.0 green:104.0/255.0 blue:170.0/255.0 alpha:1.0];
            [cell.contentView addSubview:lblName];
            [lblName release];
我在哪里做错了? 尝试了link,但无法做到正确。

2 个答案:

答案 0 :(得分:0)

+(NSString *)getTextFromChar:(unichar)character
{
    NSString *string = [NSString stringWithFormat:@"%C",character];
    return string;
}

将cString传递给此方法

答案 1 :(得分:0)

试试这个

char *cString[] = [doc.title UTF8String];//where doc.title is above text
            NSData *data = [NSData dataWithBytes:cString length:strlen(cString)];
            NSString *string = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding];
            lblName.text= string;

或只是致电

-[NSString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES].