VB icon

CString to Char Conversion

Email
Submitted on: 1/4/2015 7:10:00 PM
By: Phillip Hopkins (from psc cd)  
Level: Intermediate
User Rating: By 2 Users
Compatibility: C++ (general), Microsoft Visual C++
Views: 1392
 
     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.
 

INCLUDE files:

Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
//**************************************
//INCLUDE files for :CString to Char Conversion
//**************************************
afx.h
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
				
//**************************************
// Name: CString to Char Conversion
// Description: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.
// By: Phillip Hopkins (from psc cd)
//
// Inputs:This header file takes in a CString that you can easily provide.
//
// Returns:When complete, this function will return a pointer to a character array on the heap with the same contents as the CString that was passed into it.
//
// Assumes:This code was intended for use with MFC, and I have not really tried it outside of MFC, so if you have any experiences with this code as such, please make a comment on this code about them.
//**************************************

//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;
}


Other 1 submission(s) by this author

 


Report Bad Submission
Use this form to tell us if this entry should be deleted (i.e contains no code, is a virus, etc.).
This submission should be removed because:

Your Vote

What do you think of this code (in the Intermediate category)?
(The code with your highest vote will win this month's coding contest!)
Excellent  Good  Average  Below Average  Poor (See voting log ...)
 

Other User Comments


 There are no comments on this submission.
 

Add Your Feedback
Your feedback will be posted below and an email sent to the author. Please remember that the author was kind enough to share this with you, so any criticisms must be stated politely, or they will be deleted. (For feedback not related to this particular code, please click here instead.)
 

To post feedback, first please login.