您当前的位置: 首页 > 

鱼儿-1226

暂无认证

  • 0浏览

    0关注

    1100博文

    0收益

  • 0浏览

    0点赞

    0打赏

    0留言

私信
关注
热门博文

Vc下unicode和UTF8相互转换

鱼儿-1226 发布时间:2020-08-21 16:43:04 ,浏览量:0

在vc下使用SQLite数据库时,由于SQL语句使用utf8 编码,而CString 是unicode编码。

一, utf8 转 Unicode

CString UTF8ToUnicode(char* UTF8)

{

DWORD dwUnicodeLen;        //转换后Unicode的长度

TCHAR *pwText;            //保存Unicode的指针

CString strUnicode;        //返回值

//获得转换后的长度,并分配内存

dwUnicodeLen = MultiByteToWideChar(CP_UTF8,0,UTF8,-1,NULL,0);

pwText = new TCHAR[dwUnicodeLen];

if (!pwText)

{

return strUnicode;

}

//转为Unicode

MultiByteToWideChar(CP_UTF8,0,UTF8,-1,pwText,dwUnicodeLen);

//转为CString

strUnicode.Format(_T(“%s”),pwText);

//清除内存

delete []pwText;

//返回转换好的Unicode字串

return strUnicode;

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42

二, Unicode转utf8

size_t CDGQDialog::g_f_wctou8(char * dest_str, const wchar_t src_wchar)

{

int count_bytes = 0;

wchar_t byte_one = 0, byte_other = 0x3f; // 用于位与运算以提取位值0x3f—>00111111

unsigned char utf_one = 0, utf_other = 0x80; // 用于”位或”置标UTF-8编码0x80—>1000000

wchar_t tmp_wchar =L’0’; // 用于宽字符位置析取和位移(右移位)

unsigned char tmp_char =L’0’;

if (!src_wchar)//

return (size_t)-1;

for (;;) // 检测字节序列长度

{

if (src_wchar             
关注
打赏
1604459285
查看更多评论
0.0908s