当前位置:编程学习 > C/C++ >>

NSString编码转换

  常有编码方式有很多种,现在列出之间的转换方式

NSString To UFT-8

[cpp] 
NSString *str2 =@"深"; 
const char * s = [str2 UTF8String]; 
     出现类似这样格式的字段"\\U6df1\\U5733\\U56fd\\U5f00\\U884c01\\U673a\\U623",通常为Unicode码,转换测试函数为

[cpp] 
- (NSString *)replaceUnicode:(NSString *)unicodeStr {   
    NSString *tempStr1 = [unicodeStr stringByReplacingOccurrencesOfString:@"\\u" withString:@"\\U"];   
    NSString *tempStr2 = [tempStr1 stringByReplacingOccurrencesOfString:@"\"" withString:@"\\\""];   
    NSString *tempStr3 = [[@"\"" stringByAppendingString:tempStr2] stringByAppendingString:@"\""];   
    NSData *tempData = [tempStr3 dataUsingEncoding:NSUTF8StringEncoding];   
    NSString* returnStr = [NSPropertyListSerialization propertyListFromData:tempData   
                                                           mutabilityOption:NSPropertyListImmutable    
                                                                     format:NULL   
                                                           errorDescription:NULL];   
     
    return [returnStr stringByReplacingOccurrencesOfString:@"\\r\\n" withString:@"\n"];   
}   
    出现类似这样格式的字段"深圳银行",通常为GBK码,转换方式为
[cpp] 
NSURL *url = [NSURL URLWithString:str2]; 
    NSData *responseData = [NSData dataWithContentsOfURL:url]; 
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000); 
    NSString *responseString = [[NSString alloc] initWithData:responseData encoding:enc]; 

 


 

补充:软件开发 , C++ ,
CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,