Windows环境下Unicode编程总结
==========================
一、让VC6支持Unicode
通过使用unicode编译,软件可以适应多种情况,如何在自己的工程中添加这两种编译方式呢?下面是一个简单的步骤
1、选择“Build->Configurations”菜单
2、点击“Add”按钮,添加“Unicode Debug” copysetting from “win32 Debug”配置
添加“Unicode Release”copysetting from “win32 Release”配置 ,然后点击“OK”
4、选择“Project->Setting”菜单
5、切换到“General ”TAB页(可选)
6、修改“Win32 Unicode Debug”的Intermediate Files和Output Files为DebugU (可选)
7、修改“Win32 Unicode Release”的Intermediate Files和Output Files为ReleaseU (可选)
8、切换到“C++ ” Tab页
9、从下拉列表框中选择“Preprocessor”
10、为“Win32 Unicode Debug”和“Win32 Unicode Release”分别添加"_UNICODE,UNICODE " variables
11、切换到“link ” Tab页, 从下拉列表框中选择“output” ,设置Entry为wWinMainCRTStartup(如果为cosole程序则不需要)
12、在需要Unicode字符变量的地方用TCHAR(或WCHAR,两个是一样的)定义,如果需要ANSI则依然用char定义
13、把所有的Unicode字符串常量用L宏包起来,比如 TCHAR* szText = L"我的Text";
二、Unicode与UTF8的相互转换
///
// Convert a UTF8 string to WCHAR string.
// Caller must release the memory of pwszOutput by calling "delete[] pwszOutput ".
///
HRESULT UTF8ToWChar( char * ptszInput, WCHAR ** pwszOutput )
{
int cchOutput = 0;
if( NULL == ptszInput || NULL == pwszOutput )
{
return( E_INVALIDARG );
}
//
// Get output buffer size
//
#ifndef UNICODE
cchOutput = wcslen( ptszInput ) + 1;
#else //UNICODE
cchOutput = MultiByteToWideChar( CP_UTF8, 0, ptszInput, -1, NULL, 0 );
if( 0 == cchOutput )
{
return( HRESULT_FROM_WIN32( GetLastError() ) );
}
#endif // UNICODE
*pwszOutput = new WCHAR[ cchOutput ];
//memset( *pwszOutput, 0 , cchOutput + 1 ) ;
if( NULL == *pwszOutput)
{
return( E_OUTOFMEMORY );
}
#ifndef UNICODE
wcsncpy( *pwszOutput, ptszInput, cchOutput );
#else //UNICODE
if( 0 == MultiByteToWideChar( CP_UTF8, 0, ptszInput, -1, *pwszOutput, cchOutput ) )
{
//SAFE_ARRAYDELETE( *pwszOutput );
return( HRESULT_FROM_WIN32( GetLastError() ) );
}
#endif // UNICODE
return( S_OK );
}
///
// Convert a WCHAR string to UTF8 string.
// Caller must release the memory of ptszOutput by calling "delete[] ptszOutput ".
///
HRESULT WCharToUTF8(WCHAR * pwszInput , char ** ptszOutput)
{
int cchOutput = 0;
if( NULL == pwszInput || NULL == ptszOutput )
{
return( E_INVALIDARG );
}
//
// Get output buffer size
//
#ifndef UNICODE
cchOutput = wcslen( pwszInput ) + 1;
#else //UNICODE
cchOutput = WideCharToMultiByte( CP_UTF8, 0, pwszInput, -1, NULL, 0 ,NULL ,NULL );
if( 0 == cchOutput )
{
return( HRESULT_FROM_WIN32( GetLastError() ) );
}
#endif // UNICODE
*ptszOutput = new char[ cchOutput] ;
if( NULL == *ptszOutput)
{
return( E_OUTOFMEMORY );
}
memset( *ptszOutput , 0 , cchOutput) ;
#ifndef UNICODE
wcsncpy( *ptszOutput, pwszInput, cchOutput );
#else //UNICODE
if( 0 == WideCharToMultiByte( CP_UTF8, 0, pwszInput, -1, *ptszOutput, cchOutput,NULL ,NULL ) )
{
//SAFE_ARRAYDELETE( *ptszOutput );
return( HRESULT_FROM_WIN32( GetLastError() ) );
}
#endif // UNICODE
return( S_OK );
}
2005-8-20 17:07 UTF8与ANSI的相互转换
=====================================================
///
// Convert a UTF8 string to ANSI string.
// Caller must release the memory of pANSIOutput by calling delete[] pANSIOutput.
///
BOOL UTF8toANSI( char * pUtf8Input , char ** pANSIOutput )
{
if( NULL == pUtf8Input && NULL == pANSIOutput ) return FALSE ;
//convert to unicdoe firstly
int nchOutput = MultiByteToWideChar( CP_UTF8, 0, pUtf8Input, -1, NULL, 0 );
if( 0 == nchOutput )
return FALSE ;
WCHAR * pWcharsz = new WCHAR[ nchOutput ];
ASSERT( pWcharsz != NULL ) ;
if( NULL == pWcharsz )
return FALSE ;
memset( pWcharsz , 0 , nchOutput * sizeof(WCHAR) ) ;
nchOutput = MultiByteToWideChar( CP_UTF8, 0, pUtf8Input, -1, pWcharsz, nchOutput ) ;
if( nchOutput <= 0 )
{
delete [] pWcharsz ;
return FALSE ;
}
//convert ANSI
nchOutput = WideCharToMultiByte( CP_ACP, 0, pWcharsz, -1, NULL, 0 ,NULL ,NULL );
if( nchOutput <= 0 )
{
delete [] pWcharsz ;
return FALSE ;
}
* pANSIOutput = new char[nchOutput] ;
ASSERT( * pANSIOutput != NULL ) ;
if( pANSIOutput == 0 )
{
delete [] pWcharsz ;
return FALSE ;
}
memset( * pANSIOutput , 0 , nchOutput ) ;
nchOutput = WideCharToMultiByte( CP_ACP, 0, pWcharsz, -1, *pANSIOutput, nchOutput ,NULL ,NULL );
if( nchOutput == 0 )
{
delete [] pWcharsz ;
return FALSE ;
}
delete [] pWcharsz ;
return TRUE ;
}
///
// Convert a ANSI string to ANSI ing.
// Caller must release the memory of pUtf8Output by calling delete[] pUtf8Output.
///
BOOL ANSItoUTF8( char * pAnsiInput , char ** pUtf8Output )
{
if( NULL == pAnsiInput && NULL == pUtf8Output ) return FALSE ;
int nWcharLen = 0 ;
WCHAR * pWchar = NULL ;
//convert to unicode firstly
nWcharLen = MultiByteToWideChar( CP_ACP , 0 , pAnsiInput , -1 , NULL, 0 ) ;
pWchar = new WCHAR[nWcharLen] ;
ASSERT( pWchar != NULL ) ;
if( pWchar == NULL )
return FALSE ;
memset(pWchar, 0 , nWcharLen * sizeof(WCHAR) ) ;
nWcharLen = MultiByteToWideChar( CP_ACP , 0 , pAnsiInput ,-1 , pWchar , nWcharLen );
ASSERT( nWcharLen != 0 ) ;
if( nWcharLen == 0 )
{
delete [] pWchar ;
return FALSE;
}
// get output buffer size
int nOutput = WideCharToMultiByte( CP_UTF8, 0, pWchar, -1, NULL, 0 ,NULL ,NULL );
ASSERT( nOutput != 0 ) ;
if( 0 == nOutput )
{
delete [] pWchar ;
return FALSE ;
}
//
* pUtf8Output = new char[ nOutput] ;
ASSERT( * pUtf8Output != NULL ) ;
if( NULL == * pUtf8Output)
{
delete [] pWchar ;
return FALSE ;
}
memset( *pUtf8Output , 0 , nOutput) ;
//
nOutput = WideCharToMultiByte( CP_UTF8, 0, pWchar, -1, *pUtf8Output, nOutput,NULL ,NULL ) ;
delete [] pWchar ;
return nOutput ? TRUE : FALSE ;
}
==========================
一、让VC6支持Unicode
通过使用unicode编译,软件可以适应多种情况,如何在自己的工程中添加这两种编译方式呢?下面是一个简单的步骤
1、选择“Build->Configurations”菜单
2、点击“Add”按钮,添加“Unicode Debug” copysetting from “win32 Debug”配置
添加“Unicode Release”copysetting from “win32 Release”配置 ,然后点击“OK”
4、选择“Project->Setting”菜单
5、切换到“General ”TAB页(可选)
6、修改“Win32 Unicode Debug”的Intermediate Files和Output Files为DebugU (可选)
7、修改“Win32 Unicode Release”的Intermediate Files和Output Files为ReleaseU (可选)
8、切换到“C++ ” Tab页
9、从下拉列表框中选择“Preprocessor”
10、为“Win32 Unicode Debug”和“Win32 Unicode Release”分别添加"_UNICODE,UNICODE " variables
11、切换到“link ” Tab页, 从下拉列表框中选择“output” ,设置Entry为wWinMainCRTStartup(如果为cosole程序则不需要)
12、在需要Unicode字符变量的地方用TCHAR(或WCHAR,两个是一样的)定义,如果需要ANSI则依然用char定义
13、把所有的Unicode字符串常量用L宏包起来,比如 TCHAR* szText = L"我的Text";
二、Unicode与UTF8的相互转换
///
// Convert a UTF8 string to WCHAR string.
// Caller must release the memory of pwszOutput by calling "delete[] pwszOutput ".
///
HRESULT UTF8ToWChar( char * ptszInput, WCHAR ** pwszOutput )
{
int cchOutput = 0;
if( NULL == ptszInput || NULL == pwszOutput )
{
return( E_INVALIDARG );
}
//
// Get output buffer size
//
#ifndef UNICODE
cchOutput = wcslen( ptszInput ) + 1;
#else //UNICODE
cchOutput = MultiByteToWideChar( CP_UTF8, 0, ptszInput, -1, NULL, 0 );
if( 0 == cchOutput )
{
return( HRESULT_FROM_WIN32( GetLastError() ) );
}
#endif // UNICODE
*pwszOutput = new WCHAR[ cchOutput ];
//memset( *pwszOutput, 0 , cchOutput + 1 ) ;
if( NULL == *pwszOutput)
{
return( E_OUTOFMEMORY );
}
#ifndef UNICODE
wcsncpy( *pwszOutput, ptszInput, cchOutput );
#else //UNICODE
if( 0 == MultiByteToWideChar( CP_UTF8, 0, ptszInput, -1, *pwszOutput, cchOutput ) )
{
//SAFE_ARRAYDELETE( *pwszOutput );
return( HRESULT_FROM_WIN32( GetLastError() ) );
}
#endif // UNICODE
return( S_OK );
}
///
// Convert a WCHAR string to UTF8 string.
// Caller must release the memory of ptszOutput by calling "delete[] ptszOutput ".
///
HRESULT WCharToUTF8(WCHAR * pwszInput , char ** ptszOutput)
{
int cchOutput = 0;
if( NULL == pwszInput || NULL == ptszOutput )
{
return( E_INVALIDARG );
}
//
// Get output buffer size
//
#ifndef UNICODE
cchOutput = wcslen( pwszInput ) + 1;
#else //UNICODE
cchOutput = WideCharToMultiByte( CP_UTF8, 0, pwszInput, -1, NULL, 0 ,NULL ,NULL );
if( 0 == cchOutput )
{
return( HRESULT_FROM_WIN32( GetLastError() ) );
}
#endif // UNICODE
*ptszOutput = new char[ cchOutput] ;
if( NULL == *ptszOutput)
{
return( E_OUTOFMEMORY );
}
memset( *ptszOutput , 0 , cchOutput) ;
#ifndef UNICODE
wcsncpy( *ptszOutput, pwszInput, cchOutput );
#else //UNICODE
if( 0 == WideCharToMultiByte( CP_UTF8, 0, pwszInput, -1, *ptszOutput, cchOutput,NULL ,NULL ) )
{
//SAFE_ARRAYDELETE( *ptszOutput );
return( HRESULT_FROM_WIN32( GetLastError() ) );
}
#endif // UNICODE
return( S_OK );
}
2005-8-20 17:07 UTF8与ANSI的相互转换
=====================================================
///
// Convert a UTF8 string to ANSI string.
// Caller must release the memory of pANSIOutput by calling delete[] pANSIOutput.
///
BOOL UTF8toANSI( char * pUtf8Input , char ** pANSIOutput )
{
if( NULL == pUtf8Input && NULL == pANSIOutput ) return FALSE ;
//convert to unicdoe firstly
int nchOutput = MultiByteToWideChar( CP_UTF8, 0, pUtf8Input, -1, NULL, 0 );
if( 0 == nchOutput )
return FALSE ;
WCHAR * pWcharsz = new WCHAR[ nchOutput ];
ASSERT( pWcharsz != NULL ) ;
if( NULL == pWcharsz )
return FALSE ;
memset( pWcharsz , 0 , nchOutput * sizeof(WCHAR) ) ;
nchOutput = MultiByteToWideChar( CP_UTF8, 0, pUtf8Input, -1, pWcharsz, nchOutput ) ;
if( nchOutput <= 0 )
{
delete [] pWcharsz ;
return FALSE ;
}
//convert ANSI
nchOutput = WideCharToMultiByte( CP_ACP, 0, pWcharsz, -1, NULL, 0 ,NULL ,NULL );
if( nchOutput <= 0 )
{
delete [] pWcharsz ;
return FALSE ;
}
* pANSIOutput = new char[nchOutput] ;
ASSERT( * pANSIOutput != NULL ) ;
if( pANSIOutput == 0 )
{
delete [] pWcharsz ;
return FALSE ;
}
memset( * pANSIOutput , 0 , nchOutput ) ;
nchOutput = WideCharToMultiByte( CP_ACP, 0, pWcharsz, -1, *pANSIOutput, nchOutput ,NULL ,NULL );
if( nchOutput == 0 )
{
delete [] pWcharsz ;
return FALSE ;
}
delete [] pWcharsz ;
return TRUE ;
}
///
// Convert a ANSI string to ANSI ing.
// Caller must release the memory of pUtf8Output by calling delete[] pUtf8Output.
///
BOOL ANSItoUTF8( char * pAnsiInput , char ** pUtf8Output )
{
if( NULL == pAnsiInput && NULL == pUtf8Output ) return FALSE ;
int nWcharLen = 0 ;
WCHAR * pWchar = NULL ;
//convert to unicode firstly
nWcharLen = MultiByteToWideChar( CP_ACP , 0 , pAnsiInput , -1 , NULL, 0 ) ;
pWchar = new WCHAR[nWcharLen] ;
ASSERT( pWchar != NULL ) ;
if( pWchar == NULL )
return FALSE ;
memset(pWchar, 0 , nWcharLen * sizeof(WCHAR) ) ;
nWcharLen = MultiByteToWideChar( CP_ACP , 0 , pAnsiInput ,-1 , pWchar , nWcharLen );
ASSERT( nWcharLen != 0 ) ;
if( nWcharLen == 0 )
{
delete [] pWchar ;
return FALSE;
}
// get output buffer size
int nOutput = WideCharToMultiByte( CP_UTF8, 0, pWchar, -1, NULL, 0 ,NULL ,NULL );
ASSERT( nOutput != 0 ) ;
if( 0 == nOutput )
{
delete [] pWchar ;
return FALSE ;
}
//
* pUtf8Output = new char[ nOutput] ;
ASSERT( * pUtf8Output != NULL ) ;
if( NULL == * pUtf8Output)
{
delete [] pWchar ;
return FALSE ;
}
memset( *pUtf8Output , 0 , nOutput) ;
//
nOutput = WideCharToMultiByte( CP_UTF8, 0, pWchar, -1, *pUtf8Output, nOutput,NULL ,NULL ) ;
delete [] pWchar ;
return nOutput ? TRUE : FALSE ;
}