-
July 18th, 2007, 04:09 AM
#1
CString to BYTE array
I have a program that sends a message over a serial port and when I test it with a message like
BYTE Msg[] = "20\r";
everything is ok.
The problem is that I do not want to send "20" everytime.. The message I want to send comes from a CEdit box and I get it with
Code:
...
UpdateData(TRUE);
CString Msg;
m_Felt3.GetWindowText(Msg);
Msg.Format(_T("%s\r"),(LPCTSTR)Msg);
UpdateData(FALSE);
...
How can I change this to be a byte-array like my first example?
I also need to know the number of bytes in my message....
-
July 18th, 2007, 04:22 AM
#2
Re: CString to BYTE array
-
July 18th, 2007, 05:27 AM
#3
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?
-
July 18th, 2007, 05:52 AM
#4
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:
Code:
CString Msg;
m_Felt3.GetWindowText(Msg);
Msg.Format(_T("%s\r"),(LPCTSTR)Msg);
According to MSDN article "CString::Format":
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.
Victor Nijegorodov
-
July 18th, 2007, 05:53 AM
#5
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!
-
July 18th, 2007, 06:11 AM
#6
Re: CString to BYTE array
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);
-
July 18th, 2007, 06:16 AM
#7
Re: CString to BYTE array
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);
-
July 18th, 2007, 06:24 AM
#8
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');
Victor Nijegorodov
-
July 18th, 2007, 06:40 AM
#9
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();
-
July 18th, 2007, 07:04 AM
#10
Re: CString to BYTE array
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?
Victor Nijegorodov
-
July 19th, 2007, 12:57 AM
#11
Re: CString to BYTE array
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?
Nobody said anything about UNICODE here. No it will not convert from UNICODE to ANSI .
Will String::Format convert UNICODE to ANSI ? And have you checked if the above example will work with UNICODE turned on?
Last edited by Sahir; July 19th, 2007 at 01:09 AM.
-
July 19th, 2007, 11:00 AM
#12
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();
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|