UTF8 ANSI 相互转换的函数

来源:互联网 发布:怎样参加淘宝聚划算 编辑:程序博客网 时间:2024/06/07 12:45
#include <winnls.h>#include <malloc.h>LPCSTR AnsiToUtf8(LPCSTR Ansi){  int WLength = MultiByteToWideChar(CP_ACP, 0, Ansi, -1, NULL, 0);  LPWSTR pszW = (LPWSTR) _alloca((WLength+1) * sizeof(WCHAR));  MultiByteToWideChar(CP_ACP, 0, Ansi, -1, pszW, WLength);    int ALength = WideCharToMultiByte(CP_UTF8, 0, pszW, -1, NULL, 0, NULL, NULL);  LPSTR pszA = (LPSTR)_alloca( ALength + 1);  WideCharToMultiByte(CP_UTF8, 0, pszW, -1, pszA, ALength, NULL, NULL);    pszA[ALength] = 0;    return pszA;}LPCSTR WcharToUtf8(LPCWSTR szUnicode){  int ALength = WideCharToMultiByte(CP_UTF8, 0, szUnicode, -1, NULL, 0, NULL, NULL);  LPSTR pszA = (LPSTR)_alloca( ALength + 1);  WideCharToMultiByte(CP_UTF8, 0, szUnicode, -1, pszA, ALength, NULL, NULL);    pszA[ALength] = 0;    return pszA;}LPCSTR Utf8toAnsi( LPCSTR utf8 ){    int WLength = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, NULL );  LPWSTR pszW = (LPWSTR) _alloca( (WLength + 1) *sizeof(WCHAR) ) ;      MultiByteToWideChar(CP_UTF8, 0, utf8, -1, pszW, WLength );  pszW[WLength] = 0;    int ALength = WideCharToMultiByte(CP_ACP, 0, pszW, -1, NULL, 0, NULL, NULL);  LPSTR pszA = ( LPSTR ) _alloca ( ALength + 1 );  WideCharToMultiByte(CP_ACP, 0, pszW, -1, pszA, ALength, NULL, NULL);  pszA[ALength] = 0;    return pszA;}LPCWSTR Utf8toWchar( LPCSTR utf8 ){  int WLength = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, NULL, NULL );  LPWSTR pszW = (LPWSTR) _alloca( (WLength + 1) *sizeof(WCHAR) ) ;      MultiByteToWideChar(CP_UTF8, 0, utf8, -1, pszW, WLength );  pszW[WLength] = 0;  return pszW;}

原创粉丝点击