国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > 综合技术 > MFC转化类方法

MFC转化类方法

来源:程序员人生   发布时间:2015-07-06 10:28:06 阅读次数:2585次

1.字符转化

    1)窄字符与宽字符之间的转化

        wstring ConvertStringToWString(const std::string & str)
        {
            int nLen = (int)str.size();

            int nSize = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str.c_str(), nLen, 0, 0);

            if(nSize <= 0)
            {
                return L"";
            }

           WCHAR *pwszDst = new WCHAR[nSize+1];

           if( NULL == pwszDst)
           {
              return L"";
           }
 
           MultiByteToWideChar(CP_ACP, 0,(LPCSTR)str.c_str(), nLen, pwszDst, nSize);

           pwszDst[nSize] = 0;

           wstring wcharString;

           if( pwszDst[0] == 0xFEFF) // skip Oxfeff
           {
              for(int i = 0; i < nSize; i ++)
              pwszDst[i] = pwszDst[i+1];

             wstring wcharString1(pwszDst,nSize⑴);

             wcharString = wcharString1;
           }
           else
           {
             wstring wcharString2(pwszDst,nSize);

             wcharString = wcharString2;
           }

           delete pwszDst;

           return wcharString;
        }

 

        void ConertTCHARToString(TCHAR * ori,string * str)
        {
            int wLen = (int)(wcslen( ori ) + 1);

            int aLen=WideCharToMultiByte(CP_ACP,0,ori,wLen,NULL,0, NULL,NULL);

            char * lpa = new char [aLen];
  
            WideCharToMultiByte(CP_ACP,0,ori,wLen,lpa, aLen,NULL,NULL);

           *str = lpa;
 
            delete [] lpa;
         }

    2)wstring与UCS2之间的转化

        int ConvertWStringToUCS2(const std::wstring & WStr,std::string *UCS2Str)
        { 
            //存储为char
            char *TempBuff = new char[wcslen(WStr.c_str())*2];

            memcpy(TempBuff,WStr.c_str(),wcslen(WStr.c_str())*2);

            string HighStr="";
            string LowStr ="";
            //逐字处理
            for(UINT i = 0;i<wcslen(WStr.c_str())*2;i=i+2)
            {       
                HighStr = ConvertByteToHexStr((BYTE)TempBuff[i]);
                LowStr  = ConvertByteToHexStr((BYTE)TempBuff[i+1]);

                //变成网络序
                *UCS2Str = *UCS2Str+LowStr+HighStr;
            }
            delete [] TempBuff;

            return (int)wcslen(WStr.c_str())*2;
        }

 

         int ConvertUCS2ToWString(const std::string &UCS2Str,std::wstring * WStr)
         {
             size_t len = UCS2Str.size();
 
              if(len%4!=0 || len ==0)
              {
                  *WStr= L"";
                  return 0;
              }
              BYTE * tempbuff = new BYTE[len/2];
 
              int pos = 0;

              for(UINT i =0;i<len;i=i+4)
              {
                  //网络序变成正常序
                 tempbuff[pos+1]= ConvertHexStrToBYTE(UCS2Str.substr(i,2));
                 tempbuff[pos]= ConvertHexStrToBYTE(UCS2Str.substr(i+2,2));

                 pos= pos+2;       
              }

              //变成wstring
              unsigned short * wbuff = new unsigned short[len/4+1];

              memcpy(wbuff,tempbuff,len/2);

              wbuff[(len/4)] ='';

              (*WStr).assign((wchar_t *)wbuff);

              delete [] tempbuff;

              return (int)len/2;

        }

    3)int转化为wstring

        wstring IntToWString(const int Num)
        {
            wchar_t buff[11];
            memset(buff, 0, sizeof(buff));
  
            swprintf_s(buff,11,L"%d",Num);

            return wstring(buff);
        }

    4)16进制字串转化为int类型

        int ConvertHexStrToInt(const std::string & HexStr)
        {
            int   i = 0;

            sscanf_s(HexStr.c_str(),"%x", &i);

            return i;
        }

    5)Byte转化为16进制字串

        string ConvertByteToHexStr(const BYTE v)
        {
            char hex_chars[] = "0123456789ABCDEF";

            char t[3];

            BYTE d, r;

            d = hex_chars[v/16];

            r = hex_chars[v%16];

            sprintf_s(t,3,"%c%c", d, r);
 
            return string(t,2);
        }

 

 

 

 

 

 

 

   

 

生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生