简体   繁体   中英

iOS encode string to unicode

I have a string that both have English letters and Japanese characters. I need to convert the Japanese characters to Unicode before transmitting the data to our server. I found a lot of topic converting to UTF-8 but I have not found anything when it comes to converting to Unicode.

Side note: I also notice that if I put the string in array and use NSLog to print the array, the Japanese text is printed in Unicode.

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

This will print out: "a\\U3055" for my string data inside the array.

NSData *dataenc = [data dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *encodevalue = [[NSString alloc]initWithData:dataenc encoding:NSUTF8StringEncoding];
-(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:@"+"];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM