Jace
Jace

Reputation: 459

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.

Upvotes: 3

Views: 2776

Answers (2)

Mr.Javed Multani
Mr.Javed Multani

Reputation: 13264

-(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:@"+"];
}

Upvotes: 0

Sport
Sport

Reputation: 8945

NSData *dataenc = [data dataUsingEncoding:NSNonLossyASCIIStringEncoding];
NSString *encodevalue = [[NSString alloc]initWithData:dataenc encoding:NSUTF8StringEncoding];

Upvotes: 15

Related Questions