Advertisement
ASP_Volume3 Debugging and Error Handling #56882

CString to Char Conversion

This little bit of code is simply a method of converting "CString"'s into "char"'s. I am posting this because I was having some trouble finding any code that did this type of conversion, or at least I didn't find a method that suited my needs. Regardless, I thought I'd post this for those who may need a quick and (somewhat) easy way to convert CStrings into Chars. This code is admittedly simplistic, so don't expect anything advanced here! I have added comments for each step, despite its simplicity.

AI

KI-Zusammenfassung: This codebase represents a historical implementation of the logic described in the metadata. Our preservation engine analyzes the structure to provide context for modern developers.

Quellcode
original-source
//Takes in a CString, creates a char on the heap, and individually
//assigns each character to its respective array char, then returns
//a pointer to that array.
char * GetCharForm(CString myCString)
{
	//declare a pointer to an array of chars on the heap,
	//one for each character in the CString, plus and extra
	//one for the null terminator.
	char *tmpstring = new char[myCString.GetLength() + 1];
	//declare "i", which will be used for iterations,
	//and "m", which we set equal to the length of the
	//CString.
	int i, m=myCString.GetLength();
	//start the character-by-character loop
	for (i=0; i < m; i++)
	{
		//set this char in the array to its
		//respective character in the CString.
		tmpstring[i]=myCString.GetAt(i);
	}
	
	//Add in the null terminator at the end
	tmpstring[m+1]='\n';
	//return with our newly converted string!
	return tmpstring;
}
Upload
Originalkommentare (3)
Wiederhergestellt von der Wayback Machine