TCHAR和CHAR類型的互轉


http://blog.csdn.net/ahjxly/article/details/8494217

http://blog.csdn.net/b_h_l/article/details/7581519

http://blog.chinaunix.net/uid-339069-id-3402668.html

沒有定義UNICODE,所以它里面的字符串就是簡單用" "就行了,創建工程的時候包含了UNICODE定義,就必須對TCHAR和char進行轉換。

void TcharToChar(const TCHAR * tchar, char * _char)
{
    int iLength;
    //獲取字節長度   
    iLength = WideCharToMultiByte(CP_ACP, 0, tchar, -1, NULL, 0, NULL, NULL);
    //將tchar值賦給_char    
    WideCharToMultiByte(CP_ACP, 0, tchar, -1, _char, iLength, NULL, NULL);
}

void CharToTchar(const char * _char, TCHAR * tchar)
{
    int iLength;
    iLength = MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, NULL, 0);
    MultiByteToWideChar(CP_ACP, 0, _char, strlen(_char) + 1, tchar, iLength);
}

表明 TCHAR 與 WCHAR 屬同一類型
char szA[100];                    // ANSI string buffer
WCHAR szW[100];            // Unicode string buffer
// Normal sprintf:all strings are ANSI
sprintf(szA, "%s","ANSI Str");
// Converts Unicode string to ANSI
sprintf(szA,"%S",L"Unicode Str");
// Normal swprintf:all strings are Unicode
swprintf(szW,L"%s",L"Unicode Str");
// Converts ANSI string to Unicode
swprintf(szW,L"%S", "ANSI Str");
注意:大寫S 和小寫s 的使用

 

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM