Konvertierung int to CString
-
also mit _itow kann man integer werte in wchar_t* konvertieren.
habe ich ein CString kann auf den Buffer über GetBuffer zugegriffen werden,
aber irgendwie ist mir die sache nicht ganz klar und richtig:ist das so legitim?
_itow(value,s1->GetBuffer(sizeof(int)),10);ich denke mal das hier noch kein speicher dem Buffer zugewiesen worden ist und wenn, wie kann ich genau soviel speicher wie die Zahl groß ist dem CString zuweisen??):
für jeglichen Lösungsansatz wäre ich dankbar.
-
s1->GetBuffer(sizeof(int))
Holt einen Buffer von 4 Bytes! So groß ist eben ein int. In Textdarstellung kann ein int aber um einiges größer werden (9-10 Stellen).
Verwende doch einfach
s1->Format(_T("%d"),value);
-
http://www.c-plusplus.net/forum/viewtopic-var-t-is-39077.html
Ups, zu langsam...
-
ja gut, aber ich möchte auf das Format nicht zurückgreifen, eher die logic des CStrings verstehen und die _itow Funktion nutzen (interessehalber)
Was macht denn s1->GetBuffer(sizeof(int)) genau?
wird damit gleich dem CString speicher zugewiesen oder holt sich der CString einfach nur die ersten 4 Bytes des Buffers der im CString vorhanden ist
(was bei wchar_t ja lediglich 2 zeichen sind oder?)Also in der MSDN steht folgendes:
Getbuffer:
This method retrieves a pointer to the internal character buffer for the CString object. The returned LPTSTR is not const and thus allows direct modification of CString contents.If nMinBufLength is greater than the length of the current buffer, the call to GetBuffer will destroy the current buffer. Replace it with a buffer of the requested size, and reset the reference count to zero. If you have previously called LockBuffer on this buffer, you will lose the lock on the buffer.
nMinBufLength
Specifies the minimum size of the character buffer in characters. This value does not include space for a null terminator.
Return ValueThis is an LPTSTR pointer to the character buffer of the null-terminated object.
Remarks
If you use the pointer returned by GetBuffer to change the string contents, you must call ReleaseBuffer before using any other CString methods.The address returned by GetBuffer may not be valid after the call to ReleaseBuffer since additional CString operations may cause the CString buffer to be reallocated. The buffer will not be reallocated if you do not change the length of the CString.
The buffer memory will be freed automatically when the CString object is destroyed.
Note that, if you keep track of the string length yourself, you should not append the terminating null character. You must, however, specify the final string length when you release the buffer with ReleaseBuffer. If you do append a terminating null character, you should pass –1 for the length to ReleaseBuffer, which will perform a _tcslen on the buffer to determine its length.
Example
The following example demonstrates the use of CString::GetBuffer.
// example for CString::GetBuffer
CString s( "abcd" );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endif
LPTSTR p = s.GetBuffer( 10 );
lstrcpy( p, _T("Hello") ); // directly access CString buffer
s.ReleaseBuffer( );
#ifdef _DEBUG
afxDump << "CString s " << s << "\n";
#endifWenn ich das richtig verstehe gibt mir GetBuffer einen pointer zurück. was mit nMinBufLength gemeint ist, versteh ich nicht.
Wie kann ich den Buffer (also ohne Format-Methode) genausoviel Speicher zuweisen wie für einen integer wert benötigt wird?
-
GetBuffer returniert einen Zeiger auf einen Puffer der exakt die Größe hat, die Du anforderst. sizeof(int) ist 4, also bekommst Du einen Puffer für 4 Zeichen plus ein Zeichen für das terminiernde 0 Zeichen.
In der Ascii Darstellung einer Zahl kann ein 32bit Integer mit Vorzeichen bis zu 11 Stellen lang werden. GetBuffer(11) wäre also mindestens nötig.BTW: Es ist nicht nicht nötig die MSDN komplett zu quoten.