zoukankan      html  css  js  c++  java
  • 使用MultiByteToWideChar转换UTF8为GBK(UTF8在Windows的代码页是CP_UTF8)

    两个使用的函数:
    1,UTF8转化为Unicode,inline为了编译后更快运行,老用到了,返回字符串为了使用链式表达式
    inline WCHAR  *UTF8ToUnicode(const char *str) throw()
     {
      int i = MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,str,-1,NULL,0);         
      WCHAR   *strUnicode=new   WCHAR[i];         
      MultiByteToWideChar(CP_UTF8,MB_ERR_INVALID_CHARS,str,-1,strUnicode,i);
      return strUnicode;
      delete []strUnicode;
     }
    一定要返回WCHAR 或wchar_t类型,否则有些字符就会变成“?”,Unicode(UCS-2)是2个字节宽
     
    2,Unicode转化为UTF8,inline同上意义
    inline char *UnicodeToUTF8(const WCHAR* pText) throw()
     {
      int i= WideCharToMultiByte(CP_UTF8,0,pText,-1,NULL,0,NULL,NULL); //输入缓冲区大小是宽字符数         
      char   *strUTF8   =   new   char[i];         
      WideCharToMultiByte(CP_UTF8,0,pText,-1,strUTF8,i,NULL,NULL);
      return strUTF8;
      delete []strUTF8;
     }
     
    http://xu20cn.blog.51cto.com/274020/66117
  • 相关阅读:
    shell去重
    JDBC源码解析
    try catch finally
    URL
    域名与IP地址的联系与区别
    C++stack
    C++vector
    单链表常见面试题(C语言实现)
    数据库limit子句
    strcpy和memcpy的区别
  • 原文地址:https://www.cnblogs.com/findumars/p/6973487.html
Copyright © 2011-2022 走看看