Using the stringstream class

Add variable to string

This is a nice easy way to add values to strings and just create strings easily.

#include <sstream>
	
	stringstream ss1;
	int myInt = 54;
	double myDouble = 1.234;
	ss1 << "The int is " << myInt;
	ss1 << ", and the double is " << myDouble;		//You can add to a stringstream like this

	//If you want to then pass the stringstream to a string:
	string s1 = ss1.str();
Get variable from string
#include <sstream>

	stringstream ss1;
	int my_value;
	//float my_value;
	//double my_value;
	ss1 << "1.234";
	ss1 >> my_value;		//Will convert to int, float, double

Converting double to string with a fixed number of decimal places

//**********************************************************************************
//********** CONVERT DOUBLE TO STRING WITH FIXED NUMBER OF DECIMAL DIGITS **********
//**********************************************************************************
//#include <string.h>
//#include <iostream>
//#include <iomanip>
void convert_double_to_string_fixed_precision (double value, string *output_string, int number_of_digits)
{
	stringstream stream;
	stream << fixed << setprecision(number_of_digits) << value;	//"fixed" as otherwise scientific notation can get used or value can be rounded to less digits (yes it does happen!)
	*output_string = stream.str();
}

Modifiers

“hex”

Make values hexadecimal.  Persistent.  Reset back to normal by using “dec”.

uppercase

Make values beign turned into hex for instance uppercase.  Does not affect strings.  Persistent.  Reset with “nouppercase”.

setfill(‘0’)

Set the fill character for leading and trailing spaces.  Persistent.  Reset to default with “setfill(‘ ‘)”

setw(2)

Set the width of a value by inserting fill characters if necessary.  NOT presistent, must be used prior to each value.

Adding Leading Zero’s, Spaces, etc

//#include <iomanip>
	cout << setfill('0') << setw(4) << 15;

	//Reset the setfill persistent modifier back to normal if carrying on adding things to the stream (setw is not persistent)
	cout << setfill(' ');

Will output: 0015

BYTE Values

If you output variables and they are BYTE then you may need to use (int) to convert them to an int to display properly.

	ss1 << hex << uppercase << setfill('0') << setw(2) << (int)message_to_send_data[count];

Examples

DateTime Values
	//Create as yyyymmddhh
	stringstream ss1;
	ss1 << (tmFromDate.tm_year + 1900);							//2000 - 2099
	ss1 << setfill('0') << setw(2) << (tmFromDate.tm_mon + 1);	//1-12
	ss1 << setfill('0') << setw(2) << tmFromDate.tm_mday;			//1-31
	ss1 << setfill('0') << setw(2) << tmFromDate.tm_hour;			//0-23

	//Create in UTC format
	stringstream ss1;
	ss1 << (tmFromDate.tm_year + 1900) << "-";							//2000 - 2099
	ss1 << setfill('0') << setw(2) << (tmFromDate.tm_mon + 1) << "-";		//1-12
	ss1 << setfill('0') << setw(2) << tmFromDate.tm_mday << " ";			//1-31
	ss1 << setfill('0') << setw(2) << tmFromDate.tm_hour << ":";
	ss1 << setfill('0') << setw(2) << tmFromDate.tm_min << ":";
	ss1 << setfill('0') << setw(2) << tmFromDate.tm_sec;
Hex Values
	cout << hex << uppercase << setfill('0') << setw(2) << 0 << ":" << setw(2) << 2 << ":" << setw(2) << 15 << ":" << setw(2) << 16 << ":" << setw(2) << 254 << ":" << setw(2) << 255 << ",";		//setw() is not persistent so has to be used before each value
	cout << 15 << ",";
	cout << "heLLo1" << ",";

	//Reset the hex, setfill and uppercase persistent modifiers back to normal if carrying on adding things to the stream  (setw is not persistent)
	cout << dec << setfill(' ') << nouppercase;		
	cout << 15 << ",";
	cout << "heLLo2" << ",";
	cout << endl;

Outputs this:

00:02:0F:10:FE:FF,F,heLLo1,15,heLLo2,

NOTE:

If you output variables and they are BYTE then you will need to use (int) to convert them to an int to work properly

USEFUL?
We benefit hugely from resources on the web so we decided we should try and give back some of our knowledge and resources to the community by opening up many of our company’s internal notes and libraries through mini sites like this. We hope you find the site helpful.
Please feel free to comment if you can add help to this page or point out issues and solutions you have found, but please note that we do not provide support on this site. If you need help with a problem please use one of the many online forums.

Comments

Your email address will not be published. Required fields are marked *