首页编程widechartomultibyte,widechartomultibyte在哪个头文件

widechartomultibyte,widechartomultibyte在哪个头文件

编程之家2023-11-04168次浏览

大家好,widechartomultibyte相信很多的网友都不是很明白,包括widechartomultibyte在哪个头文件也是一样,不过没有关系,接下来就来为大家分享关于widechartomultibyte和widechartomultibyte在哪个头文件的一些知识点,大家可以关注收藏,免得下次来找不到哦,下面我们开始吧!

widechartomultibyte,widechartomultibyte在哪个头文件

widechartomultibyte在哪个头文件

函数功能:该函数映射一个unicode字符串到一个多字节字符串。

函数原型:

int WideCharToMultiByte(

UINT CodePage,//指定执行转换的代码页

DWORD dwFlags,//允许你进行额外的控制,它会影响使用了读音符号(比如重音)的字符

LPCWSTR lpWideCharStr,//指定要转换为宽字节字符串的缓冲区

widechartomultibyte,widechartomultibyte在哪个头文件

int cchWideChar,//指定由参数lpWideCharStr指向的缓冲区的字符个数

LPSTR lpMultiByteStr,//指向接收被转换字符串的缓冲区

int cchMultiByte,//指定由参数lpMultiByteStr指向的缓冲区最大值

LPCSTR lpDefaultChar,//遇到一个不能转换的宽字符,函数便会使用pDefaultChar参数指向的字符

LPBOOL pfUsedDefaultChar//至少有一个字符不能转换为其多字节形式,函数就会把这个变量设为TRUE

);

widechartomultibyte,widechartomultibyte在哪个头文件

参数:

CodePage:指定执行转换的代码页,这个参数可以为系统已安装或有效的任何代码页所给定的值。你也可以指定其为下面的任意一值:

CP_ACP:ANSI代码页;CP_MACCP:Macintosh代码页;CP_OEMCP:OEM代码页;

CP_SYMBOL:符号代码页(42);CP_THREAD_ACP:当前线程ANSI代码页;

CP_UTF7:使用UTF-7转换;CP_UTF8:使用UTF-8转换。

WideCharToMultiByte导入头文件为什么还是不能用

WideCharToMultiByte

函数功能:该函数映射一个unicode字符串到一个多字节字符串。

函数原型:

int WideCharToMultiByte(

UINT CodePage,//指定执行转换的代码页

DWORD dwFlags,//允许你进行额外的控制,它会影响使用了读音符号(比如重音)的字符

LPCWSTR lpWideCharStr,//指定要转换为宽字节字符串的缓冲区

int cchWideChar,//指定由参数lpWideCharStr指向的缓冲区的字符个数

LPSTR lpMultiByteStr,//指向接收被转换字符串的缓冲区

int cchMultiByte,//指定由参数lpMultiByteStr指向的缓冲区最大值

LPCSTR lpDefaultChar,//遇到一个不能转换的宽字符,函数便会使用pDefaultChar参数指向的字符

LPBOOL pfUsedDefaultChar//至少有一个字符不能转换为其多字节形式,函数就会把这个变量设为TRUE

);

参数:

CodePage:指定执行转换的代码页,这个参数可以为系统已安装或有效的任何代码页所给定的值。你也可以指定其为下面的任意一值:

CP_ACP:ANSI代码页;CP_MACCP:Macintosh代码页;CP_OEMCP:OEM代码页;

CP_SYMBOL:符号代码页(42);CP_THREAD_ACP:当前线程ANSI代码页;

CP_UTF7:使用UTF-7转换;CP_UTF8:使用UTF-8转换。

std:string怎么转wchar

开发语言:C++

#include<iostream>

#include<windows.h>

#include<string>

// wchar_t to string

void Wchar_tToString(std::string& szDst, wchar_t*wchar)

{

wchar_t* wText= wchar;

DWORD dwNum= WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,NULL,0,NULL,FALSE);// WideCharToMultiByte的运用

char*psText;// psText为char*的临时数组,作为赋值给std::string的中间变量

psText= new char[dwNum];

WideCharToMultiByte(CP_OEMCP,NULL,wText,-1,psText,dwNum,NULL,FALSE);// WideCharToMultiByte的再次运用

szDst= psText;// std::string赋值

delete []psText;// psText的清除

}

// string to wstring

void StringToWstring(std::wstring& szDst, std::string str)

{

std::string temp= str;

int len=MultiByteToWideChar(CP_ACP, 0,(LPCSTR)temp.c_str(),-1, NULL,0);

wchar_t* wszUtf8= new wchar_t[len+1];

memset(wszUtf8, 0, len* 2+ 2);

MultiByteToWideChar(CP_ACP, 0,(LPCSTR)temp.c_str(),-1,(LPWSTR)wszUtf8, len);

szDst= wszUtf8;

std::wstring r= wszUtf8;

delete[] wszUtf8;

}

int main(int argc, char*argv)

{

// wchar_t to string

std::string szDest;

wchar_t wText[20]={L"宽字符转换字符串实例!OK!"};

Wchar_tToString(szDest, wText);

std::cout<<"szDest:"<<szDest<<std::endl;

/**

* wchar_t定义的变量为什么不能输出呢?

*宽字符类型要本地化,否则输不出想要结果。

*本地化有三条语句可以使用,任取其一。最后一句是全局函数,前两个是wcout的一个成员函数的两种表达方式。

*/

//std::wcout.imbue(std::locale("chs"));

//std::wcout.imbue(std::locale(""));

setlocale(LC_ALL,"Chinese-simplified");

// string to wstring

std::string szSrc="字符串转换宽字符实例!OK!";

std::wstring wszDest;

StringToWstring(wszDest, szSrc);

std::wcout<<"wszDest:"<< wszDest<< std::endl;

/**

string to wchar_t

*/

string str="字符串转换宽字符实例!OK!";

std::wstring widstr= std::wstring(str.begin(), str.end());

const wchar_t*pwidstr= widstr.c_str();

//此方法简单,但是 delete wc;时会出异常

wchar_t* wc= new wchar_t[szSrc.size()];

swprintf(wc,L"%S",szSrc.c_str());//注意大写S。。

std::wcout<<"szSrc:"<< wc<<std::endl;

//delete wc;

system("PAUSE");

}

pb 调用怎么调用MultiByteToWideChar函数

为了支持Unicode编码,需要多字节与宽字节之间的相互转换。这两个系统函数在使用时需要指定代码页,在实际应用过程中遇到乱码问题,然后重新阅读《Windows核心编程》,总结出正确的用法。

WideCharToMultiByte的代码页用来标记与新转换的字符串相关的代码页。

MultiByteToWideChar的代码页用来标记与一个多字节字符串相关的代码页。

常用的代码页由CP_ACP和CP_UTF8两个。

使用CP_ACP代码页就实现了ANSI与Unicode之间的转换。

使用CP_UTF8代码页就实现了UTF-8与Unicode之间的转换。

下面是代码实现:

ANSI to Unicode

wstring ANSIToUnicode( const string str)

{

int len= 0;

len= str.length();

int unicodeLen=::MultiByteToWideChar( CP_ACP,

0,

str.c_str(),

-1,

NULL,

0);

wchar_t* pUnicode;

pUnicode= new wchar_t[unicodeLen+1];

memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));

::MultiByteToWideChar( CP_ACP,

0,

str.c_str(),

-1,

(LPWSTR)pUnicode,

unicodeLen);

wstring rt;

rt=( wchar_t*)pUnicode;

delete pUnicode;

return rt;

}

2. Unicode to ANSI

string UnicodeToANSI( const wstring str)

{

char* pElementText;

int iTextLen;

// wide char to multi char

iTextLen= WideCharToMultiByte( CP_ACP,

0,

str.c_str(),

-1,

NULL,

0,

NULL,

NULL);

pElementText= new char[iTextLen+ 1];

memset(( void*)pElementText, 0, sizeof( char)*( iTextLen+ 1));

::WideCharToMultiByte( CP_ACP,

0,

str.c_str(),

-1,

pElementText,

iTextLen,

NULL,

NULL);

string strText;

strText= pElementText;

delete[] pElementText;

return strText;

}

3. UTF-8 to Unicode

wstring UTF8ToUnicode( const string str)

{

int len= 0;

len= str.length();

int unicodeLen=::MultiByteToWideChar( CP_UTF8,

0,

str.c_str(),

-1,

NULL,

0);

wchar_t* pUnicode;

pUnicode= new wchar_t[unicodeLen+1];

memset(pUnicode,0,(unicodeLen+1)*sizeof(wchar_t));

::MultiByteToWideChar( CP_UTF8,

0,

str.c_str(),

-1,

(LPWSTR)pUnicode,

unicodeLen);

wstring rt;

rt=( wchar_t*)pUnicode;

delete pUnicode;

return rt;

}

4. Unicode to UTF-8

string UnicodeToUTF8( const wstring str)

{

char* pElementText;

int iTextLen;

// wide char to multi char

iTextLen= WideCharToMultiByte( CP_UTF8,

0,

str.c_str(),

-1,

NULL,

0,

NULL,

NULL);

pElementText= new char[iTextLen+ 1];

memset(( void*)pElementText, 0, sizeof( char)*( iTextLen+ 1));

::WideCharToMultiByte( CP_UTF8,

0,

str.c_str(),

-1,

pElementText,

iTextLen,

NULL,

NULL);

string strText;

strText= pElementText;

delete[] pElementText;

return strText;

}

widechartomultibyte和widechartomultibyte在哪个头文件的问题分享结束啦,以上的文章解决了您的问题吗?欢迎您下次再来哦!

成都网站设计公司 成都有哪些网站设计公司长沙网站推广,长沙网站推广有哪些步骤