64,691
社区成员
发帖
与我相关
我的任务
分享
#define M2WCP_ACP 0X9000000
#define W2MCP_ACP 0X9100000
#define M2WCP_UTF8 0X9065001
#define W2MCP_UTF8 0X9165001
template <class In, class Out>
Out iconv( In inbuf, Out outbuf, const int &type)
{
int nLen = 0;
int byte_type = -1; /*字节类型 区别多字节 宽字节 'M' 表示多字节 'W'表示宽字节*/
int encode_type = -1; /*编码类型 CP_ACP CP_UTF8*/
switch (type)
{
case M2WCP_ACP:
encode_type = CP_ACP;
byte_type = int('M');
break;
case W2MCP_ACP:
encode_type = CP_ACP;
byte_type = int('W');
break;
case M2WCP_UTF8:
encode_type = CP_UTF8;
byte_type = int('M');
break;
case W2MCP_UTF8:
encode_type = CP_UTF8;
byte_type = int('W');
break;
default:
break;
}
if (byte_type == int('W'))
{
nLen = WideCharToMultiByte(encode_type, 0, (LPCWSTR)inbuf.c_str(), -1, NULL, 0, NULL, NULL);
if(nLen > 0)
{
outbuf.resize(nLen, ' ');
WideCharToMultiByte(encode_type, 0, (LPCWSTR)inbuf.c_str(), -1, (LPSTR)outbuf.c_str(), nLen, NULL, NULL);
}
}
else if (byte_type == int('M'))
{
nLen = MultiByteToWideChar(encode_type, NULL, (LPCSTR)inbuf.c_str(), inbuf.length(), NULL, 0);
if (nLen > 0)
{
outbuf.resize(nLen, ' ');
MultiByteToWideChar(encode_type, NULL, (LPCSTR)inbuf.c_str(), inbuf.length(), (LPWSTR)outbuf.c_str(), nLen);
}
}
if (nLen == 0) outbuf = Out();
return outbuf;
}
/*vs2015 这么调用,告警 告警如下图*/
wstring str;
string sstr((char*)(key));
str = iconv<string&, wstring&>(sstr, str, M2WCP_UTF8);