CString::GetLength(), what does it in fact return?
-
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?
-
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?
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
-
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
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. -
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
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 calledif (nNewLength == -1)
nNewLength = lstrlen(m_pchData); // zero terminatedASSERT(nNewLength <= GetData()->nAllocLength);
GetData()->nDataLength = nNewLength;
m_pchData[nNewLength] = '\0';
}-- jlr http://jlamas.blogspot.com/[^]
-
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.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
-
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?
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.
-
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.
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?)