zoukankan      html  css  js  c++  java
  • char[]转换成wchar_t的转换方法(GNU Libc规定wchar_t为32位)

    wchar_t是C/C++的字符数据类型,是一种扩展的字符存储方式,wchar_t类型主要用在国际化程序的实现中,但它不等同于unicode编码。unicode编码的字符一般以wchar_t类型存储。char是8位字符类型,最多只能包含256种字符,许多外文字符集所含的字符数目超过256个,char型无法表示。
      wchar_t数据类型一般为16位或32位,但不同的C或C++库有不同的规定,如GNU Libc规定wchar_t为32位[1],总之,wchar_t所能表示的字符数远超char型。
      标准C++中的wprintf()函数以及iostream类库中的类和对象能提供wchar_t宽字符类型的相关操作。
    例如
      #include
      #include
      #include
      using namespace std;
      locale loc("chs"); //依环境而定,可能不同。
      int main()
      {
      wchar_t wStr[] = L"中文";
      wcout.imbue(loc);
      wcout << wStr << endl;
      return 0;
      }
    将char[]转换成wchar_t
      可以用TEXT()方法将char转换成wchar_t
      例如: wchar_t appName[5]=TEXT("test");
      方法2:
      wchar_t* c2w(const char *str)
      {
      int length = strlen(str)+1;
      wchar_t *t = (wchar_t*)malloc(sizeof(wchar_t)*length);
      memset(t,0,length*sizeof(wchar_t));
      MultiByteToWideChar(CP_ACP,0,str,strlen(str),t,length);
      return t;
      }

    http://blog.sina.com.cn/s/blog_a401a1ea0101fgxh.html

  • 相关阅读:
    leetcode297
    leetcode4
    leetcode23
    leetcode72
    leetcode239
    leetcode42
    leetcode128
    leetcode998
    SAP MM GR-based IV, 无GR不能IV?
    小科普:机器学习中的粒子群优化算法!
  • 原文地址:https://www.cnblogs.com/findumars/p/5585333.html
Copyright © 2011-2022 走看看