Thứ Bảy, 16 tháng 7, 2011

Convert CString to char* and vice versa in MFC

 Hi everybody,I am Cuong, i come from VietNam. I have just said about some topic recently. Today i want to return "convert" topic to introduce to you how to convert CString to char* and vice versa in MFC.
Do you know about it ? And is it easy?
When i had a project some months ago, i had to use it but i didn't know and i felt that it was really difficult. But now i feel it is easy because i found one of the best way to do it.
For saving your time, i am introducing now.

+ Convert CString to char*

   CString myCString = "I love Viet Nam"; // using: L"I love Viet Nam" if you want to do with Unicode format
    char myChar[1000];  // the number depends on your CString's length
    //initialize your storage buffer
    memset(myChar,0,sizeof(myChar));
    sprintf(myChar,"%S",myCString);

Actually, you can use an easier syntax if you don't use Unicode format
   myChar = myCString.GetBuffer(myCString.GetLength());

+ Convert char* to CString

  CString myCString;
  char* myChar = "I love Viet Nam"; // You mustn't insert  'L' before this quote although you can use Unicode format
  for(int i = 0; myChar[i] != NULL; i++)
  {
         myCString += myChar[i];
  }
 
Maybe there are many ways to convert this type but i think this is an easy way that you can use.
Best wishes for you!, Thanks for visiting my blog!
Do your best, the rest will come!

Không có nhận xét nào:

Đăng nhận xét

Install Kubernetes using kubeadm with two nodes master & worker

Well come back for a long time my friends! Today I want to take a note about some basic knowledge about Kubernetes. I hope that it will...