多字节与宽字节之间的转换

来源:互联网 发布:淘宝怎么买私人物品 编辑:程序博客网 时间:2024/05/16 17:38
//     WideMulti.h
#pragma  onceBOOL M2W( LPCSTR pMText,LPWSTR pWText,int wLen );BOOL W2M(LPCWSTR pWText,LPSTR pMText,int mLen);


//    wideMulti.cpp
#include "stdafx.h"#include"widemulti.h" 注意:多字节专款字节,还可以如下:cstring wText;wText.Format(_T("%s"),mText); //***********  多字节转宽字节  ********//   作用:将多字节字符串转为宽字节//   BOOL M2W( LPCSTR pMText,LPWSTR pWText,int wLen ){int mLen = MultiByteToWideChar (CP_ACP, 0, pMText, -1, NULL, 0);if (wLen<mLen){return FALSE;}MultiByteToWideChar(CP_ACP, 0, pMText, -1, pWText,mLen);return TRUE;}//**************  宽字节转多字节  ***********//   传递进来的参数:待转换的宽字节串指针,多字节串指针,多字节串的长度BOOL W2M(LPCWSTR pWText,LPSTR pMText,int mLen){//    先得到转换为多字节需要的长度int wLen= WideCharToMultiByte(CP_OEMCP,NULL,pWText,-1,NULL,0,NULL,FALSE);//    判断接收的字串长度是否 < 必须的长度if(mLen <wLen){return FALSE;}WideCharToMultiByte(CP_OEMCP,NULL,pWText,-1,pMText,wLen,NULL,FALSE);return TRUE;}