CString.Format anomaly
-
Hi I have the following code INT16 asid; // has a value of 1 CString buffer = ",Asid=" buffer.Format("%s,%x",buffer,asid); the result is ",Asid,1"; My question is why the trailing comma Thanks
-
Hi I have the following code INT16 asid; // has a value of 1 CString buffer = ",Asid=" buffer.Format("%s,%x",buffer,asid); the result is ",Asid,1"; My question is why the trailing comma Thanks
You are passing the object itself as parameter to the
Format
function. This will lead to unpredictable results. See CStringT::Format[^]:Quote:
The call will fail if the string object itself is offered as a parameter to Format.
-
You are passing the object itself as parameter to the
Format
function. This will lead to unpredictable results. See CStringT::Format[^]:Quote:
The call will fail if the string object itself is offered as a parameter to Format.
-
What trailng comma? This is what you have (and the use of <pre> tags make it so much easier to read):
INT16 asid = 1; // has a value of 1
CString buffer = ",Asid=";
buffer.Format("%s,%x",buffer,asid);
// the result is ",Asid,1";
-
So if I did CString buffer = ",Asid=" buffer.format("%2x",asid); Would I get ",Asid=01" Which is what I am looking for
ForNow wrote:
Would I get ",Asid=01"
I don't believe so, I think you'd get "01" as the format() would overwrite your initial value. How about trying.
CString buffer;
buffer.Format(",Asid=%2x", asid);"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
ForNow wrote:
Would I get ",Asid=01"
I don't believe so, I think you'd get "01" as the format() would overwrite your initial value. How about trying.
CString buffer;
buffer.Format(",Asid=%2x", asid);"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle
-
Try making a small test project and play around with the format syntax and the specifiers. Knowing it will help you with other (non-CString) areas as well.
"the debugger doesn't tell me anything because this code compiles just fine" - random QA comment "Facebook is where you tell lies to your friends. Twitter is where you tell the truth to strangers." - chriselst "I don't drink any more... then again, I don't drink any less." - Mike Mullikins uncle