Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. CString.Format anomaly

CString.Format anomaly

Scheduled Pinned Locked Moved C / C++ / MFC
question
10 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    ForNow
    wrote on last edited by
    #1

    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

    L J 2 Replies Last reply
    0
    • F ForNow

      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

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

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

      F 1 Reply Last reply
      0
      • F ForNow

        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

        J Offline
        J Offline
        Jochen Arndt
        wrote on last edited by
        #3

        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.

        F 1 Reply Last reply
        0
        • J Jochen Arndt

          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.

          F Offline
          F Offline
          ForNow
          wrote on last edited by
          #4

          So if I did CString buffer = ",Asid=" buffer.format("%2x",asid); Would I get ",Asid=01" Which is what I am looking for

          J 1 Reply Last reply
          0
          • L Lost User

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

            F Offline
            F Offline
            ForNow
            wrote on last edited by
            #5

            I meant the comma following the asid, Jochen explained my mistake

            L 1 Reply Last reply
            0
            • F ForNow

              So if I did CString buffer = ",Asid=" buffer.format("%2x",asid); Would I get ",Asid=01" Which is what I am looking for

              J Offline
              J Offline
              jeron1
              wrote on last edited by
              #6

              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

              F 1 Reply Last reply
              0
              • F ForNow

                I meant the comma following the asid, Jochen explained my mistake

                L Offline
                L Offline
                Lost User
                wrote on last edited by
                #7

                You put it there in your format string.

                F 1 Reply Last reply
                0
                • J jeron1

                  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

                  F Offline
                  F Offline
                  ForNow
                  wrote on last edited by
                  #8

                  Thanks so much I got to get into the habit of using CString

                  J 1 Reply Last reply
                  0
                  • F ForNow

                    Thanks so much I got to get into the habit of using CString

                    J Offline
                    J Offline
                    jeron1
                    wrote on last edited by
                    #9

                    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

                    1 Reply Last reply
                    0
                    • L Lost User

                      You put it there in your format string.

                      F Offline
                      F Offline
                      ForNow
                      wrote on last edited by
                      #10

                      Thanks

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups