ConVertUnicodeToUTF8

本文介绍了一个用于将Unicode字符串转换为UTF-8编码的C++函数实现。该函数通过判断Unicode字符的不同范围,采用相应的UTF-8编码规则进行转换,并将结果存储在一个std::vector容器中。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

网上有很多,改一改,自已用得顺手一点儿

 

#include <vector>


/*!
@brief unicode to utf8
@param wstrIn 输入的unicode string缓存冲
@param wLen wstrIn的大小
@param utf8_string 输出的utf8 string
@return
********************************************************************/
void ConVertUnicodeToUTF8(const unsigned short *wstrIn,int wLen, std::vector<unsigned char>& utf8_string)
{
 if(NULL == wstrIn || 0 == wLen)
  return;

 int i=0;

#define putchar(a) utf8_string.push_back(a);

 for(int j=0;(unsigned long)j<wLen;j++)
 {
  //ASSERT(i<vLen);
  unsigned short c=wstrIn[j];
  if (c < 0x80)
  {
   putchar (c);
  }
  else if (c < 0x800)
  {
   putchar (0xC0 | c>>6);
   putchar (0x80 | c & 0x3F);
  }
  else if (c < 0x10000)
  {
   putchar (0xE0 | c>>12);
   putchar (0x80 | c>>6 & 0x3F);
   putchar (0x80 | c & 0x3F);
  }
  else if (c < 0x200000)
  {
   putchar (0xF0 | c>>18);
   putchar (0x80 | c>>12 & 0x3F);
   putchar (0x80 | c>>6 & 0x3F);
   putchar (0x80 | c & 0x3F);
  }
 }
#undef putchar
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值