wchar[] 与 char[] 转换

从网上找了 将近一个小时没有找到合适的传换类型代码。自己 10 分钟就写好了,有点没自信了。懒啦!! 现在帖出来和大家分享吧, 节省大家的时间。


extern "C"
{

void __declspec(dllexport) W2C(WCHAR *pwstr,  CHAR * cstr)
{
       char *pwsSrc = (char *)pwstr;

        ULONG length =0;

        while (1)
       {
              if ( *pwsSrc != 0x00 && *(pwsSrc+1)== 0x00 )
             {
                   cstr[length++] = *pwsSrc ;
    
                   pwsSrc += 2;

              }else
             {
                   cstr[length] = 0x00;
   
                   break;
              }
        }

}


void __declspec(dllexport) C2W( CHAR * cstr, WCHAR *wstr)
{
 
         CHAR *pcstr = cstr;

        WCHAR *pwcstr = wstr;

        ULONG i = 0;
        ULONG j = 0;


         while (1)
        {
            if (pcstr[i] != 0x00)
           {
               ((CHAR *)pwcstr)[j] = pcstr[i] ;
               ((CHAR *)pwcstr)[++j] = 0x00;
  
              i++;
              j++;

            }else
           {
               ((CHAR *)pwcstr)[j] = 0x00;
              ((CHAR *)pwcstr)[++j] = 0x00;

               break;
           }  
        }
 
}

最近使用才 上面的方法使用时有局限性,  字符串中不能还有中文。

改进后的使用是:


namespace CharObj
{
 //--------------------------------
 //  多字符集转Unicode 宽字符集
 //--------------------------------
 void MutilToWide( CHAR  szMutilSrcData[], DWORD dwMutilSrcDataBufSize,
      WCHAR lpWideCharStr[], DWORD dwWideCharBufSize )
 {

     MultiByteToWideChar( CP_ACP,  0 ,  
        szMutilSrcData ,
        dwMutilSrcDataBufSize ,      
        lpWideCharStr, 
        dwWideCharBufSize ) ;
 }


 //--------------------------------
 //  Unicode 宽字符集转多字符集
 //--------------------------------
 void WideToMutil( WCHAR lpWideCharStr[], DWORD dwWideCharBufSize ,
     CHAR  szMutilSrcData[], DWORD dwMutilSrcDataBufSize )
 {

    WideCharToMultiByte( CP_ACP,     // code page
         0,     // performance and mapping flags
        lpWideCharStr,  // wide-character string
        dwWideCharBufSize  ,      // number of chars in string
        szMutilSrcData ,  // buffer for new string
        dwMutilSrcDataBufSize ,
        NULL ,    
        NULL );
 }

 

}

 

 

 

C 运行时库标准函数

mbstowcs

wcstombs

 

 

你可能感兴趣的:(c,String,null,buffer,performance)