iphoneobjective-cnsstring

How do I convert a NSString into TIS-620 encoded string


In the apple document, I can see that there's a way to convert from UTF8 string to ASCII string like this

NSData *asciiData = [theString dataUsingEncoding:NSASCIIStringEncoding allowLossyConversion:YES];
NSString *asciiString = [[NSString alloc] initWithData:asciiData encoding:NSASCIIStringEncoding];

But my app requires a TIS-620 string to post to a site so I try to do the same thing

NSData *asciiData = [newPost.header dataUsingEncoding:kCFStringEncodingMacThai allowLossyConversion:YES];
NSString *asciiString = [[NSString alloc] initWithData:asciiData encoding:kCFStringEncodingMacThai];
NSLog(@"%@", asciiString);

The output I got is like this ???????????.

Does anyone know how to convert the NSString to TIS-620 properly? Thanks so much.


Solution

  • Firstly, TIS-620 is a subset of ISO-8859-11, so you should use the string encoding kCFStringEncodingISOLatinThai instead of kCFStringEncodingMacThai.

    Also, a CF encoding cannot be directly used to decode an NSString. You need to convert the CF encoding into NS encoding first.

    NSStringEncoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingISOLatinThai);
    NSData *asciiData = [newPost.header dataUsingEncoding:enc allowLossyConversion:YES];