wchar 您所在的位置:网站首页 char数组转int数组 wchar

wchar

2023-12-25 13:30| 来源: 网络整理| 查看: 265

关于wchar_t

 

在C++标准中,wchar_t是宽字符类型,每个wchar_t类型占2个字节,16位宽。汉字的表示就要用到wchar_t 。char,我们都知道,占一个字节,8位宽。

标准C++中的wprintf()函数以及iostream类库中的类和对象能提供wchar_t宽字符类型的相关操作。

locale loc( "chs" );//定义“区域设置”为中文方式wcout.imbue( loc );//载入中文字符输入方式wchar_t str[]=L"中国";//定义宽字符数组,注意L是大写wcoutint nLen = MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, NULL, 0 );if (nLen == 0){   return NULL;}wchar_t* pResult = new wchar_t[nLen];MultiByteToWideChar( CP_ACP, MB_PRECOMPOSED, szStr, -1, pResult, nLen );return pResult;}//----------------------------------------------------------------------------------// 将 宽字节wchar_t* 转换 单字节char*inline char* UnicodeToAnsi( const wchar_t* szStr ){int nLen = WideCharToMultiByte( CP_ACP, 0, szStr, -1, NULL, 0, NULL, NULL );if (nLen == 0){   return NULL;}char* pResult = new char[nLen];WideCharToMultiByte( CP_ACP, 0, szStr, -1, pResult, nLen, NULL, NULL );return pResult;}//----------------------------------------------------------------------------------// 将单字符 string 转换为宽字符 wstringinline void Ascii2WideString( const std::string& szStr, std::wstring& wszStr ){int nLength = MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, NULL, NULL );wszStr.resize(nLength);LPWSTR lpwszStr = new wchar_t[nLength];MultiByteToWideChar( CP_ACP, 0, szStr.c_str(), -1, lpwszStr, nLength );wszStr = lpwszStr;delete [] lpwszStr;}//----------------------------------------------------------------------------------int _tmain(int argc, _TCHAR* argv[]){char*   pChar = "我喜欢char";wchar_t* pWideChar = L"我讨厌wchar_t";wchar_t   tagWideCharList[100] ;char   ch = 'A';char   tagChar[100] = {NULL};CString   cStr;std::string str;

// 注:设置语言环境以便输出WideCharsetlocale(LC_ALL,"chs");

// 注: char* 转换 wchar_t*// 注: wchar_t 未重载



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有