Re: CString to BYTE array
Re: CString to BYTE array
Thanks! But I have som problems. Get the error messages:
1>.\OvnAppDlg.cpp(235) : error C2065: '_lpw' : undeclared identifier
1>.\OvnAppDlg.cpp(235) : error C2065: '_convert' : undeclared identifier
1>.\OvnAppDlg.cpp(235) : error C2065: '_acp' : undeclared identifier
I tried the tip in the thread you linked to, to search msdn, but I cant figure it out..
What is wrong? Do i need to include a file?
Re: CString to BYTE array
As I already mentioned in this thread - Search MSDN for "String Conversion Macros"!
And you will find out that all these macros are defined in AFXPRIV.H file. It is what you have to #include.
Besides, your code is incorrect:
Quote:
Code:
CString Msg;
m_Felt3.GetWindowText(Msg);
Msg.Format(_T("%s\r"),(LPCTSTR)Msg);
According to MSDN article "CString::Format":
Quote:
The call will fail if the string object itself is offered as a parameter to Format. For example, the following code:
Code:
CString str = "Some Data";
str.Format("%s%d", str, 123); // Attention: str is also used in the parameter list.
will cause unpredictable results.
Re: CString to BYTE array
Try including "atlconv.h" or "afxpriv.h", though I am not sure that is the problem. What line are you getting the error on?
LE: Victor was faster!
Re: CString to BYTE array
Quote:
Originally Posted by EoF
Try including "atlconv.h" or "afxpriv.h", though I am not sure that is the problem. What line are you getting the error on?
LE: Victor was faster!
Your rigth. It didn't help.. I get the error on this line:
memcpy(barray, T2CA(test), dwPasswordBufferSize);
Re: CString to BYTE array
Quote:
Originally Posted by VictorN
Besides, your code is incorrect:According to MSDN article "CString::Format":
Do I understand i rigth if I think this is better?
Code:
UpdateData(TRUE);
CString Felt_3_Msg;
m_Felt3.GetWindowText(Felt_3_Msg);
UpdateData(FALSE);
CString Msg;
Msg.Format(_T("%s\r"),(LPCTSTR)Felt_3_Msg);
Re: CString to BYTE array
Yes this code is OK.
However, I'd write it more simple:
Code:
CString Msg = Felt_3_Msg + _T('\r');
Re: CString to BYTE array
If you don't mind creating an intermediate std:string , you can also do it this way.
Code:
CString cs;
edit1.GetWindowText(cs);
std::string ss = cs;
PBYTE ba = (PBYTE) ss.c_str();
Re: CString to BYTE array
Quote:
Originally Posted by Sahir
If you don't mind creating an intermediate std:string , you can also do it this way.
But how will it work if CString is a UNICODE one?
Will std:string:: operator =() also convert from UNICODE to ANSI? :confused:
Re: CString to BYTE array
Quote:
Originally Posted by VictorN
But how will it work if CString is a UNICODE one?
Will std:string:: operator =() also convert from UNICODE to ANSI? :confused:
Nobody said anything about UNICODE here. No it will not convert from UNICODE to ANSI .
Will String::Format convert UNICODE to ANSI ? :confused: And have you checked if the above example will work with UNICODE turned on?
Re: CString to BYTE array
This inst that complicated. You made a fundemental MFC error in your first example.
With CStrings, you can not use the same instance using the format method in the format arguments. The results are unpredictable.
IE:
CString Alpha, Beta;
Alpha = " Cat ";
Alpha.Format("%s", Alpha ) ; // WRONG!
Beta.Format("%s", Alpha ); // Right
Alpha = Beta;
To get it into a byte array, just cast it: (const char *)Alpha; Or if you are using the newer StdString derived version, Alpha.c_str();