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::GetLength(), what does it in fact return?

CString::GetLength(), what does it in fact return?

Scheduled Pinned Locked Moved C / C++ / MFC
question
7 Posts 5 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.
  • M Offline
    M Offline
    misterbear
    wrote on last edited by
    #1

    I was just wondering what is actually returned by this function. In the documentation I find in the description that it returns the number of characters in the string, but the return value description says that it returns the bytes of the string... which one is it?

    D T 2 Replies Last reply
    0
    • M misterbear

      I was just wondering what is actually returned by this function. In the documentation I find in the description that it returns the number of characters in the string, but the return value description says that it returns the bytes of the string... which one is it?

      D Offline
      D Offline
      David Crow
      wrote on last edited by
      #2

      It returns the number of bytes in the object. For Unicode, this is twice the number of characters.


      "One must learn from the bite of the fire to leave it alone." - Native American Proverb

      G J 2 Replies Last reply
      0
      • D David Crow

        It returns the number of bytes in the object. For Unicode, this is twice the number of characters.


        "One must learn from the bite of the fire to leave it alone." - Native American Proverb

        G Offline
        G Offline
        Graham Bradshaw
        wrote on last edited by
        #3

        Are you sure about this? I just tried it with VC6, and in a Unicode build, the code CString strText = _T("abcde"); long lLength = strText.GetLength(); ASSERT(sizeof(TCHAR) == 2); ASSERT(lLength == 5); does not ASSERT.

        D 1 Reply Last reply
        0
        • D David Crow

          It returns the number of bytes in the object. For Unicode, this is twice the number of characters.


          "One must learn from the bite of the fire to leave it alone." - Native American Proverb

          J Offline
          J Offline
          Jose Lamas Rios
          wrote on last edited by
          #4

          DavidCrow wrote: It returns the number of bytes in the object. For Unicode, this is twice the number of characters. Actually, for Unicode, the returned length is the count of WCHARs. In other words, it's always the length in TCHARs (bytes for non-Unicode, WCHARs for Unicode). So, CString(_T("Four")).GetLength() will always return 4, regardless of Unicode settings. The semantics for CString::GetLength() are the same as for the lstrlen[^] function, which is used internally by CString implementation. For instance, see the implementation for CString::ReleaseBuffer() (VC 6.0)

          void CString::ReleaseBuffer(int nNewLength)
          {
            CopyBeforeWrite(); // just in case GetBuffer was not called

          if (nNewLength == -1)
              nNewLength = lstrlen(m_pchData); // zero terminated

          ASSERT(nNewLength <= GetData()->nAllocLength);
            GetData()->nDataLength = nNewLength;
            m_pchData[nNewLength] = '\0';
          }

          -- jlr http://jlamas.blogspot.com/[^]

          1 Reply Last reply
          0
          • G Graham Bradshaw

            Are you sure about this? I just tried it with VC6, and in a Unicode build, the code CString strText = _T("abcde"); long lLength = strText.GetLength(); ASSERT(sizeof(TCHAR) == 2); ASSERT(lLength == 5); does not ASSERT.

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #5

            Graham Bradshaw wrote: Are you sure about this? No. I misread the help for GetLength(); when it was talking about MBCS, I was seeing Unicode! I guess if I had looked at some of my code first, this would have clued me in that it is the number of characters:

            ...str.GetLength() * sizeof(TCHAR);


            "One must learn from the bite of the fire to leave it alone." - Native American Proverb

            1 Reply Last reply
            0
            • M misterbear

              I was just wondering what is actually returned by this function. In the documentation I find in the description that it returns the number of characters in the string, but the return value description says that it returns the bytes of the string... which one is it?

              T Offline
              T Offline
              Tim Smith
              wrote on last edited by
              #6

              GetLength returns the following: On ASCII strings: The number of BYTES which happens to be the number of characters. On MBCS strings: The number of BYTES which is NOT the same as the number of characters. A character that requires two BYTES will contribute two to the total of GetLength. On UNICODE strings: The number of WCHARs which is NOT the same as the number characters (UCS-4). Tim Smith I'm going to patent thought. I have yet to see any prior art.

              M 1 Reply Last reply
              0
              • T Tim Smith

                GetLength returns the following: On ASCII strings: The number of BYTES which happens to be the number of characters. On MBCS strings: The number of BYTES which is NOT the same as the number of characters. A character that requires two BYTES will contribute two to the total of GetLength. On UNICODE strings: The number of WCHARs which is NOT the same as the number characters (UCS-4). Tim Smith I'm going to patent thought. I have yet to see any prior art.

                M Offline
                M Offline
                misterbear
                wrote on last edited by
                #7

                So if I want to be sure and in the end come out with a measure of the number of bytes (since I use this in conjunction with a filename buffer for a custom multiselect file dialog) what can I do? Is there some other way of finding out the length in bytes of a string? In a compile-mode independant manner? I mean it seems clear that the object itself contains a buffer so it should also internally have a count of the bytes. Is this hidden? Because this behaviour from GetLength() seems to be a bit confusing... (or maybe I am what is confused?)

                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