1. 程式人生 > >字元編碼格式的轉換

字元編碼格式的轉換

(1)將Unicode轉換為UTF8格式:

wchar_t * wsContent =L"";

int nContentLen = WideCharToMultiByte(CP_UTF8, 0, wsContent, -1, NULL, 0, NULL, NULL);

int nLen = nContentLen*sizeof(wchar_t);

char * pCdata = new char[nLen];

int ret = WideCharToMultiByte(CP_UTF8, 0, wsContent, -1, pCdata, nContentLen, NULL, NULL);

 

(2)將UTF8格式轉換為Unicode:

char* pchar = "";

int nUnicodeLen = MultiByteToWideChar(CP_UTF8, 0, pchar, -1, NULL, 0);

wchar_t * pWcdata = new wchar_t[nUnicodeLen + 1];

int ret = MultiByteToWideChar(CP_UTF8, 0, pchar, -1, pWcdata, nUnicodeLen);

 

(3)將Unicode轉換為ANSI格式:

wchar_t * wsContent =L"";

int nContentLen = WideCharToMultiByte(CP_ACP, 0, wsContent, -1, NULL, 0, NULL, NULL);

int nLen = nContentLen*sizeof(wchar_t);

char * pCdata = new char[nLen];

int ret = WideCharToMultiByte(CP_ACP, 0, wsContent, -1, pCdata, nContentLen, NULL, NULL);

 

(4)將ANSI格式轉換為Unicode:

char* pchar = "";

int nUnicodeLen = MultiByteToWideChar(CP_ACP, 0, pchar, -1, NULL, 0);

wchar_t * pWcdata = new wchar_t[nUnicodeLen + 1];

int ret = MultiByteToWideChar(CP_ACP, 0, pchar, -1, pWcdata, nUnicodeLen);