char wchar_t CString的相互轉化

來源:互聯網
上載者:User

#include <windows.h>
#include <tchar.h>
#include <locale.h>
#include <stdio.h>
#include <atlstr.h>

 

/**
 *  @param cbCharSize: lpCharStr的位元組個數,可以設為-1,如果以null結束。
 *  @param cchWCharCnt: lpWCharStr的字元個數
 * 
 *  @return: 如果成功,返回寫入lpWCharStr的字元個數;如果成功並且cchWiCharCnt==0,
 *  返回lpWideCharStr緩衝區需要的字元個數。
 *  失敗返回0。
 * 
 **/
DWORD CharToWChar_t( LPSTR lpCharStr, DWORD cbCharSize, LPWSTR lpWCharStr, DWORD cchWCharCnt)
{
 return MultiByteToWideChar( CP_ACP, 0, lpCharStr, cbCharSize, lpWCharStr, cchWCharCnt );
}

 

/**
 * @param cchWCharCnt:字元個數,可以設為-1,如果以null結束。
 * @param cbCharSize:lpCharStr串的位元組大小。

 *  @return: 如果成功,返回寫入lpCharStr的字元個數;如果成功並且cbCharSize==0,
 *  返回lpCharStr緩衝區需要的位元組個數。
 *  失敗返回0。
 *
 **/
DWORD WChar_tToChar( LPWSTR lpWCharStr, DWORD cchWCharCnt, LPSTR lpCharStr, DWORD cbCharSize)
{
 return WideCharToMultiByte(CP_ACP, 0, lpWCharStr, cchWCharCnt, lpCharStr, cbCharSize, NULL, NULL);
}

 

/**
 *  @param cbCharSize: lpCharStr串的大小
 *  @return: 返回需要的位元組個數,包括null結束符。
 **/
DWORD CStringToChar( CString cstrInput, LPSTR lpCharStr, DWORD cbCharSize)
{
#ifdef UNICODE
 //GetLength()返回wchar_t個數
 return WChar_tToChar( (LPWSTR)(LPCTSTR)cstrInput, cstrInput.GetLength(), lpCharStr, cbCharSize);
#else
 //GetLength()返回char個數
 if ( cbCharSize > cstrInput.GetLength() )
 {
  strcpy_s( lpCharStr, cbCharSize, (LPCSTR)(LPCTSTR)cstrInput );
 }
 return cstrInput.GetLength()+1;
#endif
}

 

/**
*  @param cchWCharCnt: lpCharStr串的字元wchar_t個數
*  @return: 返回需要的字元個數,包括null結束符。
 **/
DWORD CStringToWChar_t( CString cstrInput, LPWSTR lpWCharStr, DWORD cchWCharCnt)
{
#ifdef UNICODE
 if ( cchWCharCnt > cstrInput.GetLength() )
 {
  wcscpy_s( lpWCharStr, cchWCharCnt, (LPCWSTR)(LPCTSTR)cstrInput );
 }
 return cstrInput.GetLength()+1;
#else
 return CharToWChar_t( (LPSTR)(LPCTSTR)cstrInput, cstrInput.GetLength(), lpWCharStr, cchWCharCnt);
#endif
}

 

int _tmain( int argc, TCHAR * argv[] )
{
 setlocale(LC_ALL, "chs");

 char * text[100], *lpChar;
 wchar_t * lpWchar;
 int bytes;
 while ( scanf("%s", text))
 {
  CString tmp((LPSTR)text);  //裡面使用wchar_T或是char
  printf("%d/n", tmp.GetLength());
       
  bytes = CStringToChar( tmp, NULL, 0);
  lpChar = new char[bytes+1];
  CStringToChar(tmp, lpChar, bytes);
  lpChar[bytes]=0;
  printf("%s/n", lpChar);
  delete [] lpChar;

  bytes = CStringToWChar_t(tmp, NULL, 0);
  lpWchar = new wchar_t[bytes+1];
  CStringToWChar_t(tmp, lpWchar, bytes);
  lpWchar[bytes]=0;
  printf("%S/n", lpWchar);
  delete [] lpWchar;
 }
 
 return 0;
}

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.