{"id":2460,"date":"2016-01-20T20:53:45","date_gmt":"2016-01-20T20:53:45","guid":{"rendered":"https:\/\/raspberry-projects.com\/pi\/?p=2460"},"modified":"2018-09-12T13:57:37","modified_gmt":"2018-09-12T13:57:37","slug":"using-strings-extracting","status":"publish","type":"post","link":"https:\/\/raspberry-projects.com\/pi\/programming-in-c\/strings\/string-class\/using-strings-extracting","title":{"rendered":"Using Strings-Extracting"},"content":{"rendered":"<p>\n&nbsp;\n<\/p>\n<h4>\nGet Section Of A String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tsome_other_string = my_string.substr(8, string::npos);    \/\/Copy from character 8 to the end\r\n<\/code><\/pre>\n<h4>\nGet String After String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tmy_string = my_string.substr((my_string.find(&quot;inet &quot;) + 5), string::npos);\r\n<\/code><\/pre>\n<h4>\nGet String Before Character<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tmy_string = my_string.substr(0, my_string.find(&#39;\/&#39;));\t\t\/\/Will return existing string if character not found\r\n<\/code><\/pre>\n<h4>\nConvert ASCII string of hex values to binary array example code<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\/\/----- CONVERT ASCII STRING TO BINAY ARRAY -----\r\nstring my_source_string = &quot;&gt; 04 3E 2B 02 01 00 00 B7 82 25 3F 23 AC 1F 02 01 06 03 03 F1 FF 17 16 A8 89 7C 96 39 3C 4A A0 B3 EF 43 0D A8 21 F3 54 00 00 00 00 C5 64&quot;;\r\nconst int MY_ARRAY_LENGTH = 44;\r\nchar *p_buffer;\r\nchar high_char;\r\nchar low_char;\r\nBYTE my_binary_array[MY_ARRAY_LENGTH];\r\nint count;\r\n\t\r\n\tcount = 0;\r\n\tif (my_source_string[0] != &#39;&gt;&#39;)\r\n\t\treturn;\r\n\tp_buffer = (char*)hcidump_event.c_str();\r\n\tp_buffer++;\r\n\twhile (1)\r\n\t{\r\n\t\tp_buffer++;\r\n\t\t\r\n\t\tif (*p_buffer == 0)\r\n\t\t\treturn;\t\t\t\t\/\/Wasn&#39;t long enough so can&#39;t be iBeacon\r\n\r\n\t\tif (*p_buffer == &#39; &#39;)\r\n\t\t\tcontinue;\r\n\t\t\r\n\t\tif (*p_buffer == &#39;\\n&#39;)\r\n\t\t\tcontinue;\r\n\t\r\n\t\t\/\/We have next 2 digit value\r\n\t\thigh_char = *p_buffer++;\r\n\t\tlow_char = *p_buffer;\r\n\t\tif (\r\n\t\t(high_char &lt; &#39;0&#39;) || (high_char &gt; &#39;9&#39;) &amp;&amp;\r\n\t\t(high_char &lt; &#39;A&#39;) || (high_char &gt; &#39;F&#39;) &amp;&amp;\r\n\t\t(high_char &lt; &#39;a&#39;) || (high_char &gt; &#39;f&#39;)\r\n\t\t)\r\n\t\t{\r\n\t\t\treturn;\r\n\t\t}\r\n\t\t\r\n\t\tif (\r\n\t\t(low_char &lt; &#39;0&#39;) || (low_char &gt; &#39;9&#39;) &amp;&amp;\r\n\t\t(low_char &lt; &#39;A&#39;) || (low_char &gt; &#39;F&#39;) &amp;&amp;\r\n\t\t(low_char &lt; &#39;a&#39;) || (low_char &gt; &#39;f&#39;)\r\n\t\t)\r\n\t\t{\r\n\t\t\treturn;\r\n\t\t}\r\n\t\tmy_binary_array[count++] = convert_ascii_hex_to_byte(high_char, low_char);\r\n\t\t\r\n\t\tif (count &gt;= MY_ARRAY_LENGTH)\r\n\t\t\tbreak;\t\t\t\/\/We have all the values we want\r\n\t} \/\/while (1)\r\n\/\/We have our array of values\r\n<\/code><\/pre>\n<h4>\n&nbsp;<br \/>\n<\/h4>\n<p>\n&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&nbsp; Get Section Of A String some_other_string = my_string.substr(8, string::npos); \/\/Copy from character 8 to the end Get String After String my_string = my_string.substr((my_string.find(&quot;inet &quot;) + 5), string::npos); Get String Before Character my_string = my_string.substr(0, my_string.find(&#39;\/&#39;)); \/\/Will return existing string if character not found Convert ASCII string of hex values to binary array example code [&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],"tags":[],"class_list":["post-2460","post","type-post","status-publish","format-standard","hentry","category-string-class"],"_links":{"self":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2460","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=2460"}],"version-history":[{"count":3,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2460\/revisions"}],"predecessor-version":[{"id":2918,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/posts\/2460\/revisions\/2918"}],"wp:attachment":[{"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/media?parent=2460"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/categories?post=2460"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/raspberry-projects.com\/pi\/wp-json\/wp\/v2\/tags?post=2460"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}