Important alert: (current site time 7/15/2013 5:58:06 PM EDT)
 

VB icon

^string conversions (string2char, char2string)^

Email
Submitted on: 1/7/2003 12:17:28 AM
By: NDawg 
Level: Intermediate
User Rating: By 4 Users
Compatibility: C, C++ (general), Microsoft Visual C++, Borland C++, UNIX C++
Views: 26069
 
     This code will convert between string and char* datatypes. This code is very simplistic (3 lines of code for each conversion and that's including the function header). So far I have found absolutely no problems with these functions. NOTE: this code will NOT work if you do not include the proper files below (standard VC++ library). I am fairly certain these will work on other compilers, but I have only tested them on VC++ 6.0 The reason I developed these was because I did not know how to append characters to a char* type, but strings you just use +=. Since then, I have obviously figured out there are other methods to append characters on string. In my opinion, if you are doing a lot of appending, I would rather just use += rather than call a function everytime. Just my 2 cents.
 
code:
Can't Copy and Paste this?
Click here for a copy-and-paste friendly version of this code!
 
Terms of Agreement:   
By using this code, you agree to the following terms...   
  1. You may use this code in your own programs (and may compile it into a program and distribute it in compiled format for languages that allow it) freely and with no charge.
  2. You MAY NOT redistribute this code (for example to a web site) without written permission from the original author. Failure to do so is a violation of copyright laws.   
  3. You may link to this code from another website, but ONLY if it is not wrapped in a frame. 
  4. You will abide by any additional copyright restrictions which the author may have placed in the code or code's description.
				
//**************************************
// Name: ^string conversions (string2char, char2string)^
// Description:This code will convert between string and char* datatypes. This code is very simplistic (3 lines of code for each conversion and that's including the function header). So far I have found absolutely no problems with these functions.
NOTE: this code will NOT work if you do not include the proper files below (standard VC++ library).
I am fairly certain these will work on other compilers, but I have only tested them on VC++ 6.0
The reason I developed these was because I did not know how to append characters to a char* type, but strings you just use +=. Since then, I have obviously figured out there are other methods to append characters on string. In my opinion, if you are doing a lot of appending, I would rather just use += rather than call a function everytime. Just my 2 cents.
// By: NDawg
//
// Inputs:both functions take a string input.
char* string2char(string input)
string char2string(char* input)
depending on which way you are converting, you input the other kind of string.
//
// Returns:The functions return the opposite string type as inputted.
//
// Assumes:I assume you have basic knowledge of strings and how they work.
//
//This code is copyrighted and has// limited warranties.Please see http://www.Planet-Source-Code.com/vb/scripts/ShowCode.asp?txtCodeId=5512&lngWId=3//for details.//**************************************

#include <string>
#include <iostream.h>
using std::string;
//PROTOTYPES
char* string2char(string& input);
string char2string(char* input);
int main() {
	//here are some examples of use
	//NOTE: if you include iostream rather than iostream.h, you can
	//output string types directly with cout. If you do this, you
	//must also insert "using namespace std;" below the includes
	string string1 = "this is a string";
	char * string2 = "this is also a string";
	string temp = char2string(string2);
	cout << string2char(string1) << endl << string2char(temp) << endl;
	return 0;
}
/*
this function is actually just a typecasting... basically a dummy entry
if you can remember to use string(char*), you can avoid calling a
function
*/
string char2string(char* input) {
	return string(input);
}
//function that returns a non const char*
char* string2char(string input) {
	return const_cast<char*>(input.c_str());
}


Other 14 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
1/8/2003 3:19:16 AMjefe

I don't think your string2char() function is exactly safe. I'd recommend returning a pointer to what str::string::c_str() returns. Reason being that you're not guaranteed that std::string's implementation actually keeps the null terminator at the end of the internal character array.
(If this comment was disrespectful, please report it.)

 

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.