ICU4C 的获得:
从网址:
可以下载 windows 所需的库和头文件,当然还有 DLL 文件 :)
本文测试用的 4.2 版来源于:
ucnv_convert
头文件:
库文件:
函数声明:
- U_STABLE
int32_t U_EXPORT2 - ucnv_convert(const
char *toConverterName, -
const char *fromConverterName, -
char *target, -
int32_t targetCapacity, -
const char *source, -
int32_t sourceLength, -
UErrorCode *pErrorCode); -
- //
下面是 ICU4C 4.2 的测试代码 -
const char *toConverterName= "utf8"; -
const char *fromConverterName = "gb2312"; -
char target [100]; -
int32_t targetCapacity = 100; -
const char *source="呵呵"; -
int32_t sourceLength = -1; -
UErrorCode ErrorCode = U_ZERO_ERROR; // 文档中说该值必须初始化为U_ZERO_ERROR,其实如果不初始化该值而且转换中没有出错时该值是不会被填写的,即未初始化前的值 -
int ret = ucnv_convert(toConverterName, // utf8 或 utf-8 效果是一样的 -
fromConverterName, // gbk 与 gb2312 好像区别不大,用 gbk 可能会好些 -
target, -
targetCapacity, // 详见下面 -
source, -
sourceLength, // 长度为 -1 表明 NULL 终止的字符串 -
&ErrorCode); // 该值不能为空,否则函数返回值为 0 并不做任何转换 -
// 返回值 ret 为整个源字符串 source 转换后的长度(即使 targetCapacity 空间不足也是如此)
// targetCapacity 与 转换所需空间的关系:
如果targetCapacity小于转换所需空间:
1. ret 的值为整个源串 source 转换后的字节数,该值不含 0 结尾的长度
2. target 中会填充 targetCapacity 所指定的转换的字节数,并且不会有 0 填充 target 的结尾
3. ErrorCode 会被置为
如果targetCapacity等于转换所需空间:
1. ret 的值为整个源串 source 转换后的字节数,该值不含 0 结尾的长度
2. target 中会填充 targetCapacity 所指定的转换的字节数,并且不会有 0 填充 target 的结尾
3. ErrorCode 会被置为
如果targetCapacity大于转换所需空间:
1. ret 的值为整个源串 source 转换后的字节数,该值不含 0 结尾的长度
2. target 中会填充 ret 的字节数, target[ret] 处被填为 0 表示 target 字符串结束
3. ErrorCode 会被置为
其他: