2014-02-14 109 views
3

我有一個字符串,都有英文字母和日文字符。在將數據傳輸到我們的服務器之前,我需要將日文字符轉換爲Unicode。我發現很多主題轉換爲UTF-8,但在轉換爲Unicode時我還沒有發現任何東西。iOS編碼字符串到unicode

備註:我還注意到,如果我將數組中的字符串放在數組中並使用NSLog打印數組,則日文文本將以Unicode格式打印。

NSString *data = @"aさ"; 
NSArray *array = [NSArray arrayWithObjects: data, other data..., nil]; 
NSLog(@"%@", array); 

這將打印出:「a \ U3055」爲我的字符串數據在數組中。

+0

你可能看這個.. http://stackoverflow.com/questions/10305488/nsstring-to-const-char-convert-with-greek-characters – Dinesh

+0

的NSLog和說明應僅用於調試輸出:) – larva

回答

15
NSData *dataenc = [data dataUsingEncoding:NSNonLossyASCIIStringEncoding]; 
NSString *encodevalue = [[NSString alloc]initWithData:dataenc encoding:NSUTF8StringEncoding]; 
0
-(NSString *)StringWithData:(NSString *)Encoded { 

NSString *decoded = (__bridge_transfer NSString *)CFURLCreateStringByReplacingPercentEscapesUsingEncoding(NULL, (CFStringRef)Encoded, CFSTR(""), kCFStringEncodingUTF8); 
return [decoded stringByReplacingOccurrencesOfString:@"+" withString:@" "]; 
} 

- (NSString *)EncodceStringWithData:(NSString *)Encoded { 

NSString *urlEncoded = (NSString *)CFBridgingRelease(CFURLCreateStringByAddingPercentEscapes(NULL, (CFStringRef)Encoded, NULL, (CFStringRef)@"!*'\"();:@&=+$,/?%#[]% ", kCFStringEncodingUTF8)); 
return [urlEncoded stringByReplacingOccurrencesOfString:@"%20" withString:@"+"]; 
}