{"id":2017,"date":"2015-06-09T10:40:05","date_gmt":"2015-06-09T10:40:05","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2017"},"modified":"2020-08-03T08:08:31","modified_gmt":"2020-08-03T08:08:31","slug":"using-strings-adding-values","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/strings\/string-class\/using-strings-adding-values","title":{"rendered":"Using Strings-Values"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Using the stringstream class<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">Add variable to string<\/h5>\n\n\n\n<p>This is a nice easy way to add values to strings and just create strings easily.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sstream>\n\t\n\tstringstream ss1;\n\tint myInt = 54;\n\tdouble myDouble = 1.234;\n\tss1 &lt;&lt; \"The int is \" &lt;&lt; myInt;\n\tss1 &lt;&lt; \", and the double is \" &lt;&lt; myDouble;\t\t\/\/You can add to a stringstream like this\n\n\t\/\/If you want to then pass the stringstream to a string:\n\tstring s1 = ss1.str();<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Get variable from string<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sstream>\n\n\tstringstream ss1;\n\tint my_value;\n\t\/\/float my_value;\n\t\/\/double my_value;\n\tss1 &lt;&lt; \"1.234\";\n\tss1 >> my_value;\t\t\/\/Will convert to int, float, double<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Converting double to string with a&nbsp;fixed number of decimal places<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/**********************************************************************************\n\/\/********** CONVERT DOUBLE TO STRING WITH FIXED NUMBER OF DECIMAL DIGITS **********\n\/\/**********************************************************************************\n\/\/#include &lt;string.h>\n\/\/#include &lt;iostream>\n\/\/#include &lt;iomanip>\nvoid convert_double_to_string_fixed_precision (double value, string *output_string, int number_of_digits)\n{\n\tstringstream stream;\n\tstream &lt;&lt; fixed &lt;&lt; setprecision(number_of_digits) &lt;&lt; value;\t\/\/\"fixed\" as otherwise scientific notation can get used or value can be rounded to less digits (yes it does happen!)\n\t*output_string = stream.str();\n}<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Modifiers<\/h4>\n\n\n\n<p>&#8220;hex&#8221;<\/p>\n\n\n\n<p>Make values hexadecimal.&nbsp; Persistent.&nbsp; Reset back to normal by using &#8220;dec&#8221;.<\/p>\n\n\n\n<p>uppercase<\/p>\n\n\n\n<p>Make values beign turned into hex for instance uppercase.&nbsp; Does not affect strings.&nbsp; Persistent.&nbsp; Reset with &#8220;nouppercase&#8221;.<\/p>\n\n\n\n<p>setfill(&#8216;0&#8217;)<\/p>\n\n\n\n<p>Set the fill character for leading and trailing spaces.&nbsp; Persistent.&nbsp; Reset to default with &#8220;setfill(&#8216; &#8216;)&#8221;<\/p>\n\n\n\n<p>setw(2)<\/p>\n\n\n\n<p>Set the width of a value by inserting fill characters if necessary.&nbsp; NOT presistent, must be used prior to each value.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Adding Leading Zero&#8217;s, Spaces, etc<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\/\/#include &lt;iomanip>\n\tcout &lt;&lt; setfill('0') &lt;&lt; setw(4) &lt;&lt; 15;\n\n\t\/\/Reset the setfill persistent modifier back to normal if carrying on adding things to the stream (setw is not persistent)\n\tcout &lt;&lt; setfill(' ');<\/code><\/pre>\n\n\n\n<p>Will output: 0015<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">BYTE Values<\/h4>\n\n\n\n<p>If you output variables and they are BYTE then you may&nbsp;need to use (int) to convert them to an int to display properly.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tss1 &lt;&lt; hex &lt;&lt; uppercase &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; (int)message_to_send_data&#91;count];<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Examples<\/h4>\n\n\n\n<h5 class=\"wp-block-heading\">DateTime Values<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\t\/\/Create as yyyymmddhh\n\tstringstream ss1;\n\tss1 &lt;&lt; (tmFromDate.tm_year + 1900);\t\t\t\t\t\t\t\/\/2000 - 2099\n\tss1 &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; (tmFromDate.tm_mon + 1);\t\/\/1-12\n\tss1 &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; tmFromDate.tm_mday;\t\t\t\/\/1-31\n\tss1 &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; tmFromDate.tm_hour;\t\t\t\/\/0-23\n\n\t\/\/Create in UTC format\n\tstringstream ss1;\n\tss1 &lt;&lt; (tmFromDate.tm_year + 1900) &lt;&lt; \"-\";\t\t\t\t\t\t\t\/\/2000 - 2099\n\tss1 &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; (tmFromDate.tm_mon + 1) &lt;&lt; \"-\";\t\t\/\/1-12\n\tss1 &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; tmFromDate.tm_mday &lt;&lt; \" \";\t\t\t\/\/1-31\n\tss1 &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; tmFromDate.tm_hour &lt;&lt; \":\";\n\tss1 &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; tmFromDate.tm_min &lt;&lt; \":\";\n\tss1 &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; tmFromDate.tm_sec;<\/code><\/pre>\n\n\n\n<h5 class=\"wp-block-heading\">Hex Values<\/h5>\n\n\n\n<pre class=\"wp-block-code\"><code>\tcout &lt;&lt; hex &lt;&lt; uppercase &lt;&lt; setfill('0') &lt;&lt; setw(2) &lt;&lt; 0 &lt;&lt; \":\" &lt;&lt; setw(2) &lt;&lt; 2 &lt;&lt; \":\" &lt;&lt; setw(2) &lt;&lt; 15 &lt;&lt; \":\" &lt;&lt; setw(2) &lt;&lt; 16 &lt;&lt; \":\" &lt;&lt; setw(2) &lt;&lt; 254 &lt;&lt; \":\" &lt;&lt; setw(2) &lt;&lt; 255 &lt;&lt; \",\";\t\t\/\/setw() is not persistent so has to be used before each value\n\tcout &lt;&lt; 15 &lt;&lt; \",\";\n\tcout &lt;&lt; \"heLLo1\" &lt;&lt; \",\";\n\n\t\/\/Reset the hex, setfill and uppercase persistent modifiers back to normal if carrying on adding things to the stream  (setw is not persistent)\n\tcout &lt;&lt; dec &lt;&lt; setfill(' ') &lt;&lt; nouppercase;\t\t\n\tcout &lt;&lt; 15 &lt;&lt; \",\";\n\tcout &lt;&lt; \"heLLo2\" &lt;&lt; \",\";\n\tcout &lt;&lt; endl;<\/code><\/pre>\n\n\n\n<p>Outputs this:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>00:02:0F:10:FE:FF,F,heLLo1,15,heLLo2,<\/code><\/pre>\n\n\n\n<p>NOTE:<\/p>\n\n\n\n<p><strong><em>If you output variables\u00a0and they are BYTE then you will need to use (int) to convert them to an int to work properly<\/em><\/strong><\/p>\n","protected":false},"excerpt":{"rendered":"<p>Using the stringstream class Add variable to string This is a nice easy way to add values to strings and just create strings easily. Get variable from string Converting double to string with a&nbsp;fixed number of decimal places Modifiers &#8220;hex&#8221; Make values hexadecimal.&nbsp; Persistent.&nbsp; Reset back to normal by using &#8220;dec&#8221;. uppercase Make values beign [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[97,141],"tags":[],"class_list":["post-2017","post","type-post","status-publish","format-standard","hentry","category-string-class","category-stringstream"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2017","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/comments?post=2017"}],"version-history":[{"count":19,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2017\/revisions"}],"predecessor-version":[{"id":3404,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2017\/revisions\/3404"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2017"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2017"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2017"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}