CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Guest

    Problem with CString.Format("\xFF\x00\EA") ; Help....

    I'm trying to send a string command in hex notation to a com port. The string is formatted as follow:

    CString.Format("\xFF\x00\FA")

    The Problem is \x00 = Null in ascii (string terminator) thus only FF was sent. I need the hex 00 to be part of the string command. How do I go about formatting the string so that it would be hex FF 00 FA ? Is there a better way to formatt a string in hex notation to send to serial port?

    Thank you much.


  2. #2
    Join Date
    May 1999
    Posts
    19

    Re: Problem with CString.Format("\xFF\x00\EA") ; Help....

    You should use BSTR string type. It contains length information at the beginning of the string and then the data. Data can contain anything, no problem whether its FF or 00.

    [email protected]
    B'lore
    India.



  3. #3
    Join Date
    May 1999
    Location
    Farnborough, Hants, England
    Posts
    710

    Re: Problem with CString.Format("\xFF\x00\EA") ; Help....

    If you just use the CString as a buffer (CString::GetBuffer(), then when you've finished with it CString::ReleaseBuffer() ), that will happily allow you to store NULLs, providing your comms. function will accept (char *) as the parameter (which is what GetBuffer() returns).

    --
    Jason Teagle
    [email protected]

  4. #4
    Guest

    Re: Problem with CString.Format("\xFF\x00\EA") ; Help....

    I've never used BSTR. How do you declare/store data to it?


  5. #5
    Join Date
    May 1999
    Posts
    19

    Re: Problem with CString.Format("\xFF\x00\EA") ; Help....

    The problem with CString is that, while it does handle 0x00 chars, it does not handle them very well. In this case, you are supplying a NULL terminated string as an argument to Format(). Since the second char is NULL, Format thinks the string is 1 char long.

    If you do this


    MyString.Format("\xFFQ\FA"); // 3 char string
    MyString.SetAt(1, 0x00); // Replace 2nd char




    you will create your desired string.

    The MyString will contain 3 chars, even though some functions will not work so well. E.g. Trim() thinks it has 1 char. I forget whether GetLength() uses the stored length or not.


  6. #6
    Join Date
    May 1999
    Posts
    20

    Re: Problem with CString.Format("\xFF\x00\EA") ; Help....

    Two ideas:
    1) This might be one of those very rare occaisons where you have to stoop to using old-fassioned vanilla "C". The old
    char buf[3];
    buf[0]=0xff;
    buf[1]=0;
    buf[2]=0xFA;

    2) or for the C++ pureists
    CString sBuf(0, 3);
    sBuf.SetAt(0, Oxff);
    sBuf.SetAt(1, Ox00);
    sBuf.SetAt(2, Oxfa);

    Just a thought


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