UNICODE ANSI转换

个人感觉ANSI -> UNICODE,编码方式为: 如ANSI  0X4A   -> UNICODE 0X4A00 不知道对不?

两个UNICODE ANSI转换函数:

void UnicodeToAnsi(WCHAR* lpString,char *szAnsi)

{

 size_t len = wcslen(lpString)*2;

char *buf = (char *)malloc(len);

 size_t i = wcstombs(buf,lpString,len);

strcpy(szAnsi,buf);

free(buf);

}

void AnsiTounicode(LPCSTR lpString,WCHAR * lpUni)

{

 PWSTR pWideCharStr;  // 指向UNICODE字符串的指针

int nLenOfWideCharStr;  // UNICODE字符串的长度

// 获取ANSI字符串的长度

nLenOfWideCharStr = MultiByteToWideChar(CP_ACP, 0, lpString, -1, NULL, 0);

pWideCharStr = (PWSTR)HeapAlloc(GetProcessHeap(), 0, nLenOfWideCharStr * sizeof(WCHAR));

// 转换ANSI字符串为UNICODE字符串

MultiByteToWideChar(CP_ACP, 0, lpString, -1, pWideCharStr, nLenOfWideCharStr);

wcscpy(lpUni,pWideCharStr);

 // 释放分配的字符串

 HeapFree(GetProcessHeap(), 0, pWideCharStr);

}

文章出处:http://www.diybl.com/course/3_program/c++/cppjs/200865/122058.html

posted @ 2009-02-21 17:45  含青血匕  阅读(1038)  评论(0编辑  收藏  举报