1. 程式人生 > >CString、wchar和char相互轉換(轉載)

CString、wchar和char相互轉換(轉載)

CString   Mid(   int   nFirst,   int   nCount   )   const;
nCount指明你要多少位。

CString   s(   "abcdef "   );
for   (int   i=0;   i   <   s.GetLength();   i++)
{
    TCHAR   c   =   s.GetAt(i);
    //....
}

1.標頭檔案中要定義巨集; 
      #define   UNICODE 
      #define   _UNICODE 
   
2.char轉換成wchar 
      const   char   *pFilePathName   =   "c:\\aa.dll"; 
      int   nLen   =   strlen(pFilePathName)   +   1; 
      int   nwLen   =   MultiByteToWideChar(CP_ACP,   0,   pFilePathName,   nLen,   NULL,   0); 
   
      TCHAR   lpszFile[256]; 
      MultiByteToWideChar(CP_ACP,   0,   pFilePathName,   nLen,   lpszFile,   nwLen); 
   
3.wchar轉換成char 
        char   *pFilePathName; 
        TCHAR   lpszFile[256]; 
      _tcscpy(lpszFile,   _T("c:\\aa.dll")); 
   
      int   nLen   =   wcslen(wstr)+1;    
      WideCharToMultiByte(CP_ACP,   0,   lpszFile,   nLen,   pFilePathName,   2*nLen,   NULL,   NULL);


 


char*和CString轉換


CString 是一種很特殊的 C++ 物件,它裡面包含了三個值:一個指向某個資料緩衝區的指標、一個是該緩衝中有效的字元記數(它是不可存取的,是位於 CString 地址之下的一個隱藏區域)以及一個緩衝區長度。有效字元數的大小可以是從0到該緩衝最大長度值減1之間的任何數(因為字串結尾有一個NULL字元)。字元記數和緩衝區長度被巧妙隱藏。


(1) char*轉換成CString


  若將char*轉換成CString,除了直接賦值外,還可使用CString::Format進行。例如:


         char chArray[] = "Char test";
         TCHAR * p = _T("Char test");( 或LPTSTR p = _T("Char test");)
         CString theString = chArray;
         theString.Format(_T("%s"), chArray);
         theString = p;


(2) CString轉換成char*


  若將CString類轉換成char*(LPSTR)型別,常常使用下列三種方法:


  方法一,使用強制轉換。例如:


       CString theString( (_T("Char test "));
        LPTSTR lpsz =(LPTSTR)(LPCTSTR)theString;


  方法二,使用strcpy。例如:


       CString theString( (_T("Char test "));
       LPTSTR lpsz = new TCHAR[theString.GetLength()+1];
        _tcscpy(lpsz, theString);


  需要說明的是,strcpy(或可移值的_tcscpy)的第二個引數是 const wchar_t* (Unicode)或const char* (ANSI),系統編譯器將會自動對其進行轉換。


  方法三,使用CString::GetBuffer。


        如果你需要修改 CString 中的內容,它有一個特殊的方法可以使用,那就是 GetBuffer,它的作用是返回一個可寫的緩衝指標。 如果你只是打算修改字元或者截短字串,例如:
       CString s(_T("Char test "));
        LPTSTR p = s.GetBuffer();


        LPTSTR dot = strchr(p, ''.'');


         // 在這裡新增使用p的程式碼


          if(p != NULL)


         *p = _T('\0');
         s.ReleaseBuffer();    // 使用完後及時釋放,以便能使用其它的CString成員函式


         在 GetBuffer 和 ReleaseBuffer 之間這個範圍,一定不能使用你要操作的這個緩衝的 CString 物件的任何方法。因為 ReleaseBuffer 被呼叫之前,該 CString 物件的完整性得不到保障。

相關推薦

CStringwcharchar相互轉換(轉載

CString   Mid(   int   nFirst,   int   nCount   )   const; nCount指明你要多少位。 CString   s(   "abcdef "   ); for   (int   i=0;   i   <  

[轉]CStringwcharchar相互轉換

1.標頭檔案中要定義巨集;       #define   UNICODE       #define   _UNICODE    2.char轉換成wchar       const   char   *pFilePathName   =   "c://aa.dll";  

CStringwcharchar相互轉換

1.標頭檔案中要定義巨集;       #define   UNICODE       #define   _UNICODE    2.char轉換成wchar       const   char   *pFilePathName   =   "c://aa.dll";       int   nLen 

CStringwcharchar型別的相互轉換轉載

1.標頭檔案中要定義巨集;       #define   UNICODE       #define   _UNICODE    2.char轉換成wchar       const   char   *pFilePathName   =   "c://aa.dll";       int   nLen 

[C#] Byte[]ImageBitmap 之間的相互轉換[轉載]

/// <summary>        /// 將圖片Image轉換成Byte[]        /// </summary> &nb

jni中byte[]char*相互轉換

id= (*env)->GetFieldID(env, objectClass, filedName, "[B"); jbyteArray dataArray=(jbyteArray)((*env)-> GetObjectField(env,object,id));

QT:QStringQByteArraychar *的轉換

第一種,資料流的方式,這裡只說從QByteArray轉向QString。 QByteArray encodedString = "xxx"; QTextCodec *codec = QTextCodec::codecForName("KOI8-R"); QString st

MFC TCHAR CHAR相互轉換

沒有定義UNICODE,所以它裡面的字串就是簡單用" "就行了,建立工程的時候包含了UNICODE定義,就必須對TCHAR和char進行轉換。 首先是把TCHAR轉為char //將TCHAR轉

編碼轉換(ASCIIUnicodeUnicode中文相互轉換)

參照http://tool.chinaz.com/Tools/Unicode.aspx,用C#做一個編碼轉換的小工具。程式碼如下:     /// <summary>     /// 編碼轉換工具     /// 作者:Sunny906     /// 日期:2

stringconst char* char* char[]相互轉換

其他 相互 str 了解 相互轉換 AC cell include 打印 string、const char*、 char* 、char[]四者類型經常會需要轉化。 一:轉化總結形式如下: 使用時,要對源格式和目標格式進行初始化。源格式賦值為具體的內容,目標格式賦值為空。

oracle date 字串 相互轉換字串:拼接替換擷取查詢

表結構:  插入date型別資料: 插入date資料方法是 to_date(字串,日期格式) ,sysdate 是當前系統時間,sysdate+1 表示在當前時間基礎上加1天 將字串型別 轉date 儲存到date型別中: select * from TEST_DAT

QStringintcharQByteArray直接的相互轉換

一、int 轉 QString int m=1; QString b; b=QString::number(m) 二、QString 轉int QString a=”1111” int b; b=a.toInt() 三、char 轉換為 QString char a=’

CStringCTimeCOleDateTime轉換

嚴以律己,寬以待人. 三思而後行. GMail/GTalk: yanglinbo#google.com; MSN/Email: tx7do#yahoo.com.cn; QQ: 3 0 3 3 9 6 9 2 0 .

unicode WCHAR 與多字符集char相互轉換

多字符集轉UNICODE字符集: //mbs(char) to wcs (CString) //多字符集轉為UNICODE字符集 CString mbs2wcs(LPCSTR mbstr) { CString cstr = L""; size_t aLen = str

char*wchar_t*相互轉換

問題描述  新增其它類庫時,有兩個函式的實參與形參型別不符,就是char*和wchar_t*沒有相互轉換就使用了,具體情況如下所示: error C2664: “strcpy”: 不能將引數 2 從“WCHAR [1]”轉換為“const char *”與指向的型

json物件字串的相互轉換(轉載)

//使用json中的parser方法轉換; var str='{"name":"fendouer", "age":23}'; //這是一個json字串'' var ob=JSON.parse(str) ; //返回一個新物件 console.log(ob.nam

C# byte10進制16進制相互轉換

ray converter href byte[] csharp byte odin html odi 原文:C# byte和10進制、16進制相互轉換 var SRMP = new byte[4]; Array.Copy(Encoding.UTF8.GetBytes(

NSDateNSString相互轉換

date 部分 當前 sda 英文 none 剛剛在線 轉換 微信公眾號 不積小流無以成江海,不及矽步無以至千裏。做iOS開發也是這樣。平時寫代碼要多收集一些實用的代碼,以免每次都從新來一遍,不僅浪費時間,也影響開發效率。 有人把做app形容成蓋房子。碼農

Android中 BitmapDrawable相互轉換的方法

canvas board null height .com factory oar tool pla 1、Drawable --> Bitmap [java] view plain copy Bitmap drawable2Bitmap(Drawabl

數據集JSON相互轉換

tostring 解析 con orm sys tail while string XML 使用DELPHI原生類實現數據集和JSON相互轉換 JSON二要素:數組和對象。對象可以包含數組,數組可以包含對象。無層數限制。OLEVARIANT也類似,OLEVARIANT的一