//
// CCEncoding.h
// CCFC
//
// Created by xichen on 11-12-18.
// Copyright 2011 ccteam. All rights reserved.
//
#import <Foundation/Foundation.h>
// "陈曦" 的unicode编码为:
// 0x48 0x96 0xE6 0x66
// UTF8编码为:
// 0xE9 0x99 0x88 0xE6 0x9B 0xA6
// GB18030编码为:
// 0xB3 0xC2 0xEA 0xD8
@interface CCEncoding : NSObject
{
}
// log all encodings
+ (void)logAllEncodings;
// unicode ---> UTF8
+ (const char *)UnicodeToUTF8:(NSString *)src;
// UTF8 ---> unicode
+ (NSString *)UTF8ToUnicode:(const char *)src;
// GB18030 ---> unicode
+ (NSString *)GB18030ToUnicode:(const char *)src;
// unicode ---> GB18030
+ (const char *)UnicodeToGB18030:(NSString *)src;
// GB18030 ---> UTF8
// GB18030 --> Unicode --> UTF8
+ (const char *)GB18030ToUTF8:(const char *)src;
// UTF8 ---> GB18030
// UTF8 --> Unicode --> GB18030
+ (const char *)UTF8ToGB18030:(const char *)src;
// Unicode cStr to Unicode NSString
// you should make sure the cStr begins with 0xFF, 0xFE(the unicode flag).
+ (NSString *)UnicodeCStrToUnicode:(const char *)cStr withLen:(int)len;
@end
//
// CCEncoding.m
// CCFC
//
// Created by xichen on 11-12-18.
// Copyright 2011 ccteam. All rights reserved.
//
#import "CCEncoding.h"
@implementation CCEncoding
// log all encodings
+ (void)logAllEncodings
{
const NSStringEncoding *encodings = [NSString availableStringEncodings];
NSMutableString *str = [[NSMutableString alloc] init];
NSStringEncoding encoding;
while ((encoding = *encodings++) != 0)
{
[str appendFormat:@"%@ is %x\n", [NSString localizedNameOfStringEncoding:encoding], encoding];
}
NSLog(@"str is %@", str);
[str release];
NSLog(@"defaultCStringEncoding is %d", [NSString defaultCStringEncoding]);
}
// unicode ---> UTF8
+ (const char *)UnicodeToUTF8:(NSString *)src
{
return [src UTF8String];
}
// UTF8 ---> unicode
+ (NSString *)UTF8ToUnicode:(const char *)src
{
return [NSString stringWithCString:src encoding:NSUTF8StringEncoding];
}
// GB18030 ---> unicode
+ (NSString *)GB18030ToUnicode:(const char *)src
{
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
return [NSString stringWithCString:src encoding:enc];
}
// unicode ---> GB18030
+ (const char *)UnicodeToGB18030:(NSString *)src
{
NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
return [src cStringUsingEncoding:enc];
}
// GB18030 ---> UTF8
// GB18030 --> Unicode --> UTF8
+ (const char *)GB18030ToUTF8:(const char *)src
{
NSString *unicode = [self GB18030ToUnicode:src];
return [self UnicodeToUTF8:unicode];
}
// UTF8 ---> GB18030
// UTF8 --> Unicode --> GB18030
+ (const char *)UTF8ToGB18030:(const char *)src
{
NSString *unicode = [self UTF8ToUnicode:src];
return [self UnicodeToGB18030:unicode];
}
// Unicode cStr to Unicode NSString
// you should make sure the cStr begins with 0xFF, 0xFE(the unicode flag).
+ (NSString *)UnicodeCStrToUnicode:(const char *)cStr withLen:(int)len
{
NSData *data = [NSData dataWithBytes:cStr length:len];
return [[[NSString alloc] initWithData:data encoding:NSUnicodeStringEncoding] autorelease];
}
@end
微风不燥,阳光正好,你就像风一样经过这里,愿你停留的片刻温暖舒心。
我是程序员小迷(致力于C、C++、Java、Kotlin、Android、Shell、JavaScript、TypeScript、Python等编程技术的技巧经验分享),若作品对您有帮助,请关注、分享、点赞、收藏、在看、喜欢,您的支持是我们为您提供帮助的最大动力。
欢迎关注。助您在编程路上越走越好!
标签:src,GB18030,const,ios,char,NSString,unicode,CCEncoding From: https://blog.csdn.net/cxsjabcabc/article/details/7167704