Click to See Complete Forum and Search --> : Problem with CString.Format("\xFF\x00\EA") ; Help....


May 11th, 1999, 06:52 PM
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.

Ashley Antony
May 11th, 1999, 11:14 PM
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.

Ashley.Antony@in.bosch.com
B'lore
India.

Jason Teagle
May 12th, 1999, 02:50 AM
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).

May 13th, 1999, 02:33 PM
I've never used BSTR. How do you declare/store data to it?

Mark Messer
May 13th, 1999, 03:19 PM
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.

acrown
May 13th, 1999, 04:23 PM
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