iOS gbk与utf8 互转


// gbk -> utf8
- (NSString *)gbkToUtf8:(Byte *)cString
{
    NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    
    NSData *pageData = [NSData dataWithBytes:cString length:strlen((Byte *)cString)];
    NSString *string = [[NSString alloc] initWithData:pageData encoding:encoding];
    
    return string;
}

// utf8 -> gbk
- (NSData *)utf8ToGbk:(NSString *)string
{
    NSStringEncoding encoding = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingUTF8);
    return [string dataUsingEncoding:encoding];
}












你可能感兴趣的:(iOS gbk与utf8 互转)