c++宽字符串、窄字符串 互转

来源:互联网 发布:穿越火线手游淘宝商城 编辑:程序博客网 时间:2024/04/30 15:39

Unicode字符集环境下亲测可用:

//------------------------------------------------------------------宽字符串转换到窄字符串char* pC = NULL;   wchar_t wStr[20] = L"宽字符串";  int iLen = WideCharToMultiByte( CP_ACP,0,wStr,-1,NULL,0,NULL,NULL); if( iLen > 0 ){    pC = ( char* )HeapAlloc( GetProcessHeap() ,0 ,iLen );    if( !pC ) return;    WideCharToMultiByte( CP_ACP ,0 ,wStr ,-1 ,pC ,iLen ,NULL ,NULL );      printf( "%s \n", pC );    HeapFree( GetProcessHeap() ,0 ,pC );}//------------------------------------------------------------------窄字符串转换到宽字符串char cStr[20] = "这是窄字符串";  wchar_t* pWideString = NULL;  int iLenWide = MultiByteToWideChar( CP_ACP ,0 ,cStr ,-1 ,NULL ,0 ); if ( iLenWide > 0 ){    pWideString = ( wchar_t* )malloc( iLenWide * sizeof(wchar_t) );    if( !pWideString ) return 0;    MultiByteToWideChar( CP_ACP ,0 ,cStr ,-1 ,pWideString ,iLenWide );     MessageBox( NULL, pWideString , 0 , 0 );      free( pWideString ); }
0 0
原创粉丝点击