char与Tchar之间相互装换

来源:互联网 发布:c语言实验温度转换 编辑:程序博客网 时间:2024/05/19 03:30

char与TCHAR之间的转化主要用到函数MultiByteToWideChar和WideCharToMultiByte

char转TCHAR
如果不是Unicode字符集,就不需要转换,直接复制即可,如果不确定是否使用Unicode字符集,可以这样写

char strUsr[10] = "Hello";  TCHAR Name[100];  #ifdef UNICODE      MultiByteToWideChar(CP_ACP, 0, strUsr, -1, Name, 100);  #else      strcpy(Name, strUsr);  #endif  

TCHAR转char

char* ConvertLPWSTRToLPSTR (LPWSTR lpwszStrIn)  {      LPSTR pszOut = NULL;      if (lpwszStrIn != NULL)      {          int nInputStrLen = wcslen (lpwszStrIn);          // Double NULL Termination          int nOutputStrLen = WideCharToMultiByte (CP_ACP, 0, lpwszStrIn, nInputStrLen, NULL, 0, 0, 0) + 2;          pszOut = new char [nOutputStrLen];          if (pszOut)          {              memset (pszOut, 0x00, nOutputStrLen);              WideCharToMultiByte(CP_ACP, 0, lpwszStrIn, nInputStrLen, pszOut, nOutputStrLen, 0, 0);          }      }      return pszOut;  }  
std::string Unicode2Utf8(const std::wstring& widestring){    int utf8size = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, NULL, 0, NULL, NULL);    if (utf8size == 0)    {        throw std::exception("Error in conversion.");    }    std::vector<char> resultstring(utf8size);    int convresult = ::WideCharToMultiByte(CP_UTF8, 0, widestring.c_str(), -1, &resultstring[0], utf8size, NULL, NULL);    if (convresult != utf8size)    {        throw std::exception("La falla!");    }    return std::string(&resultstring[0]);}BOOL StringToWString(const std::string &str, std::wstring &wstr){    int nLen = (int)str.length();    wstr.resize(nLen, L' ');    int nResult = MultiByteToWideChar(CP_ACP, 0, (LPCSTR)str.c_str(), nLen, (LPWSTR)wstr.c_str(), nLen);    if (nResult == 0)        return FALSE;    return TRUE;}std::wstring UTF8ToUnicode(const std::string& str){    int  len = 0;    len = str.length();    int  unicodeLen = ::MultiByteToWideChar(CP_UTF8,        0,        str.c_str(),        -1,        NULL,        0);    wchar_t *  pUnicode;    pUnicode = new  wchar_t[unicodeLen + 1];    memset(pUnicode, 0, (unicodeLen + 1)*sizeof(wchar_t));    ::MultiByteToWideChar(CP_UTF8,        0,        str.c_str(),        -1,        (LPWSTR)pUnicode,        unicodeLen);    std::wstring  rt;    rt = (wchar_t*)pUnicode;    delete  pUnicode;    return  rt;}void chartostring(char * buffer,int num){    char temp[50],tmp;    int a,i = 0,j = 0;    do    {        a = num % 10;        temp[i++] = a + '0';        num /= 10;    } while (num);    temp[i--] = '\0';    while (i != j && j <= i)    {        tmp = temp[i];        temp[i] = temp[j];        temp[j] = tmp;        i--;        j++;    }    strcat_s(buffer, 200, temp);}string GBKToUTF8(const std::string& strGBK)  {      string strOutUTF8 = "";      WCHAR * str1;      int n = MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, NULL, 0);      str1 = new WCHAR[n];      MultiByteToWideChar(CP_ACP, 0, strGBK.c_str(), -1, str1, n);      n = WideCharToMultiByte(CP_UTF8, 0, str1, -1, NULL, 0, NULL, NULL);      char * str2 = new char[n];      WideCharToMultiByte(CP_UTF8, 0, str1, -1, str2, n, NULL, NULL);      strOutUTF8 = str2;      delete[]str1;      str1 = NULL;      delete[]str2;      str2 = NULL;      return strOutUTF8;  }  string UTF8ToGBK(const std::string& strUTF8)  {      int len = MultiByteToWideChar(CP_UTF8, 0, strUTF8.c_str(), -1, NULL, 0);      unsigned short * wszGBK = new unsigned short[len + 1];      memset(wszGBK, 0, len * 2 + 2);      MultiByteToWideChar(CP_UTF8, 0, (LPCTSTR)strUTF8.c_str(), -1, wszGBK, len);      len = WideCharToMultiByte(CP_ACP, 0, wszGBK, -1, NULL, 0, NULL, NULL);      char *szGBK = new char[len + 1];      memset(szGBK, 0, len + 1);      WideCharToMultiByte(CP_ACP,0, wszGBK, -1, szGBK, len, NULL, NULL);      //strUTF8 = szGBK;      std::string strTemp(szGBK);      delete[]szGBK;      delete[]wszGBK;      return strTemp;  } 
0 0