{"id":2019,"date":"2015-06-09T11:07:56","date_gmt":"2015-06-09T11:07:56","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2019"},"modified":"2020-07-24T08:55:10","modified_gmt":"2020-07-24T08:55:10","slug":"stringstream-general","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/strings\/stringstream\/stringstream-general","title":{"rendered":".stringstream General"},"content":{"rendered":"\n<p>stringstream is a handy class that operates on strings. &nbsp;You can think it a file loaded into something resembling a string, or alternatively&nbsp;as a sort of string that you can write to and read from like a file. It&#8217;s not exactly either of those things, but its a simplified explanation.<\/p>\n\n\n\n<h4 class=\"wp-block-heading\">An example of a&nbsp;stringstream<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>#include &lt;sstream>\n\t\n\tstringstream ss1;\n\tss1 &lt;&lt; \"Hello, value is \" &lt;&lt; myInt;\n\tss1 &lt;&lt; \" total\";\n\n\t\/\/To pass the stringstream to a string:\n\tstring s1 = ss1.str();\n\n\t\/\/To pass the stringstream to a char*:\n\tstring s1 = ss1.str().c_str();<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Using variables is a stringstream<\/h4>\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<\/code><\/pre>\n\n\n\n<p><em>BYTE &#8211; note you need to use (int) in front of BYTE values, they are not handled correctly without it!<\/em><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">Converting a&nbsp;stringstream into a value<\/h4>\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\">Clear&nbsp;stringstream<\/h4>\n\n\n\n<p>You need to use both of these (or just create a new stringstream of course):<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tss1.clear();\n\tss1.str(std::string());\n\n\n\tss1.clear();\t\t\t\t\/\/Resets the internal error flags (if not called the stringstream object may think it's in an error state (such as EOF), even if the underlying string has been changed).\n\tss1.str(std::string());\t\t\/\/Clear out the old string. A bit faster than using: ss1.str(\"\")<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Byte&nbsp;uint8_t values<\/h4>\n\n\n\n<p>You need to add (int) in front of byte or char values to get them to convert otherwise they will be dealt with as a character (char)<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>\tss1 &lt;&lt; (int)my_byte_value;<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Length<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>\tss1.str().length<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>stringstream is a handy class that operates on strings. &nbsp;You can think it a file loaded into something resembling a string, or alternatively&nbsp;as a sort of string that you can write to and read from like a file. It&#8217;s not exactly either of those things, but its a simplified explanation. An example of a&nbsp;stringstream Using [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[141],"tags":[],"class_list":["post-2019","post","type-post","status-publish","format-standard","hentry","category-stringstream"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2019","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=2019"}],"version-history":[{"count":9,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2019\/revisions"}],"predecessor-version":[{"id":3330,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2019\/revisions\/3330"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2019"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2019"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2019"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}