ICU4C函数ucnv_convert

ICU4C 的获得:
从网址: http://site.icu-project.org/download
可以下载 windows 所需的库和头文件,当然还有 DLL 文件 :)
本文测试用的 4.2 版来源于: http://icu-project.org/download/4.2.html

ucnv_convert 函数:
头文件: unicode\ucnv.h
库文件: lib\icuuc.lib

函数声明:

[cpp]  view plain copy
  1. U_STABLE int32_t U_EXPORT2  
  2. ucnv_convert(const char *toConverterName,  
  3.              const char *fromConverterName,  
  4.              char *target,  
  5.              int32_t targetCapacity,  
  6.              const char *source,  
  7.              int32_t sourceLength,  
  8.              UErrorCode *pErrorCode);  
  9.   
  10. // 下面是 ICU4C 4.2 的测试代码  
  11.  const char *toConverterName= "utf8" 
  12.  const char *fromConverterName "gb2312" 
  13.  char target [100];  
  14.  int32_t targetCapacity 100;  
  15.  const char *source="呵呵" 
  16.  int32_t sourceLength -1;  
  17.  UErrorCode ErrorCode U_ZERO_ERROR; // 文档中说该值必须初始化为U_ZERO_ERROR,其实如果不初始化该值而且转换中没有出错时该值是不会被填写的,即未初始化前的值  
  18.  int ret ucnv_convert(toConverterName, // utf8 或 utf-8 效果是一样的  
  19.   fromConverterName, // gbk 与 gb2312 好像区别不大,用 gbk 可能会好些  
  20.   target,     
  21.   targetCapacity, // 详见下面  
  22.   source,  
  23.   sourceLength, // 长度为 -1 表明 NULL 终止的字符串  
  24.   &ErrorCode);  // 该值不能为空,否则函数返回值为 并不做任何转换  
  25.     // 返回值 ret 为整个源字符串 source 转换后的长度(即使 targetCapacity 空间不足也是如此)  


 

// targetCapacity 与 转换所需空间的关系:
如果targetCapacity小于转换所需空间:
1. ret 的值为整个源串 source 转换后的字节数,该值不含 0 结尾的长度
2. target 中会填充 targetCapacity 所指定的转换的字节数,并且不会有 0 填充 target 的结尾
3. ErrorCode 会被置为 U_BUFFER_OVERFLOW_ERROR

如果targetCapacity等于转换所需空间:
1. ret 的值为整个源串 source 转换后的字节数,该值不含 0 结尾的长度
2. target 中会填充 targetCapacity 所指定的转换的字节数,并且不会有 0 填充 target 的结尾
3. ErrorCode 会被置为 U_STRING_NOT_TERMINATED_WARNING

如果targetCapacity大于转换所需空间:
1. ret 的值为整个源串 source 转换后的字节数,该值不含 0 结尾的长度
2. target 中会填充 ret 的字节数, target[ret] 处被填为 0 表示 target 字符串结束
3. ErrorCode 会被置为 U_ZERO_ERROR

其他:
 target 可被置为 NULL (当然此时 targetCapacity 应该为 0),
 此时:
 1. ret 返回转换所需空间(该值不含 0 结尾的长度)
 2. ErrorCode 会被置为 U_BUFFER_OVERFLOW_ERROR
 可以使用这种方法来计算转换所需空间,但感觉不是太值 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值