CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Jun 2007
    Location
    Norway
    Posts
    43

    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....

  2. #2
    Join Date
    May 2005
    Location
    Oradea, Romania
    Posts
    190

    Re: CString to BYTE array


  3. #3
    Join Date
    Jun 2007
    Location
    Norway
    Posts
    43

    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?

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  5. #5
    Join Date
    May 2005
    Location
    Oradea, Romania
    Posts
    190

    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!

  6. #6
    Join Date
    Jun 2007
    Location
    Norway
    Posts
    43

    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);

  7. #7
    Join Date
    Jun 2007
    Location
    Norway
    Posts
    43

    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);

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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

  9. #9
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    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();

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    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?
    Victor Nijegorodov

  11. #11
    Join Date
    Aug 2002
    Location
    Kerala
    Posts
    1,183

    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?
    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.

  12. #12
    Join Date
    Sep 2004
    Posts
    1,361

    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
  •  





Click Here to Expand Forum to Full Width

Featured