C++ 中字符串操作--宽窄字符转换的实例详解
C++中字符串操作--宽窄字符转换的实例详解
MultiByteToWideChar
intMultiByteToWideChar( _In_UINTCodePage, _In_DWORDdwFlags, _In_LPCSTRlpMultiByteStr, _In_intcbMultiByte, _Out_opt_LPWSTRlpWideCharStr, _In_intcchWideChar ); 参数描述: CodePage:常用CP_ACP、CP_UTF8 dwFlags:0 lpMultiByteStr[in]: 指向待转换字符串。 cbMultiByte[in]: lpMultiByteStr"以字节规格计算"的大小。 设置0,函数失败; 设置-1,函数处理整个字符串,包括\0字符串,导致宽字符串也会带有\0字符,返回的长度也包含\0的长度; 设置>0,根据是否包含\0,返回的结果也会相应调整。 lpWideCharStr[out,optional]: 指向接收宽字符串的缓冲区。 cchWideChar[in]: lpWideCharStr指向的缓冲区"以字符规格计算"的大小。 设置0,使lpWideCharStr无效,并使得函数返回所需"以字符规格计算"的大小。
Code:
intrequiredBufSize=MultiByteToWideChar(CP_ACP,0,src,-1,NULL,0); if(requiredBufSize>0) { WCHAR*pBuffer=newWCHAR[requiredBufSize]; MultiByteToWideChar(CP_ACP,0,src,-1,pBuffer,requiredBufSize); }
WideCharToMultiByte
intWideCharToMultiByte( _In_UINTCodePage, _In_DWORDdwFlags, _In_LPCWSTRlpWideCharStr, _In_intcchWideChar, _Out_opt_LPSTRlpMultiByteStr, _In_intcbMultiByte, _In_opt_LPCSTRlpDefaultChar, _Out_opt_LPBOOLlpUsedDefaultChar ); 参数描述: lpDefaultChar[in,optional]:NULL lpUsedDefaultChar[out,optional]:NULL 其它参数参考MultiByteToWideChar
Code:
intrequiredBufSize=WideCharToMultiByte(CP_ACP,0,src,-1,NULL,0,NULL,NULL); if(requiredBufSize>0) { char*pBuffer=newchar[requiredBufSize]; WideCharToMultiByte(CP_ACP,0,src,-1,pBuffer,requiredBufSize,NULL,NULL); }
如有疑问请留言或者到本站社区交流讨论,感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!