Windows字符编码转换

利用ATL中的字符串装换宏,对Unicode和非Unicode编译环境下的字符进行宏定义封装,可以在ANSI、Unicode以及UTF-8之间进行快速转换。

#if (defined _UNICODE) || (defined UNICODE)
#ifndef _tstring   
#define _tstring wstring
#endif
#endif

#include <atlbase.h>
#define fcA2U(ASTR) (LPSTR)ATL::CW2A((ATL::CA2W(ASTR)), CP_UTF8)
#define fcW2U(WSTR) (LPSTR)ATL::CW2A((WSTR), CP_UTF8)
#define fcU2A(USTR) (LPSTR)ATL::CW2A(ATL::CA2W((USTR), CP_UTF8))
#define fcU2W(USTR) (LPWSTR)ATL::CA2W((USTR), CP_UTF8)
#define fcA2W(ASTR) (LPWSTR)ATL::CA2W(ASTR)
#define fcW2A(WSTR) (LPSTR)ATL::CW2A(WSTR)
#define fcA2A(ASTR) (LPSTR)(ASTR)
#define fcW2W(WSTR) (LPWSTR)(WSTR)
#if (defined _UNICODE) || (defined UNICODE)
#define fcU2T fcU2W
#define fcT2U fcW2U
#define fcA2T fcA2W
#define fcT2A fcW2A
#define fcW2T fcW2W
#define fcT2W fcW2W
#else
#define fcU2T fcU2A
#define fcT2U fcA2U
#define fcA2T fcA2A
#define fcT2A fcA2A
#define fcW2T fcW2A
#define fcT2W fcA2W
#endif

猜你喜欢

转载自blog.csdn.net/wizardtoh/article/details/80504923