{"id":190,"date":"2010-04-28T18:16:01","date_gmt":"2010-04-28T18:16:01","guid":{"rendered":"https:\/\/ibex.tech\/visualcpp\/?p=190"},"modified":"2022-02-17T06:24:05","modified_gmt":"2022-02-17T06:24:05","slug":"convert-bytes-to-a-string","status":"publish","type":"post","link":"https:\/\/ibex.tech\/visualcpp\/strings\/convert-bytes-to-a-string","title":{"rendered":"Convert Bytes To A String"},"content":{"rendered":"<pre>\r\n<code>\r\n\tarray&lt;Byte&gt; ^RxData = gcnew array&lt;Byte&gt;(2);\r\n\tRxData[0] = &#39;A&#39;;\r\n\tRxData[0] = &#39;B&#39;;\r\n\tString ^sTemp;\r\n\r\n\tsTemp = System::Text::Encoding::UTF8-&gt;GetString(RxData);\r\n<\/code><\/pre>\n<p>\nN.B. Make the array the length of the string &#8211; do not use a 0x00 terminating byte. If you do you get a resulting string but you can&#39;t add any new characters to it (e.g. MyString += &quot;abc&quot;) because the null has been taken into it. You can use this to remove them:\n<\/p>\n<pre>\r\n<code>\tsTemp = sTemp-&gt;Replace(&quot;\\0&quot;, &quot;&quot;);\r\n<\/code><\/pre>\n<h4>\nConverting A Single Char \/ Byte<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tByte Value = 0x31;\r\n\tSomeString += (Char)Value;\t\t\/\/&lt;&lt;Note the capital C\r\n<\/code><\/pre>\n<p>\n<code>Alternative method - convert it to an array (a silly solution but it works!) <\/code>\n<\/p>\n<pre>\r\n<code>\r\n<code>\r\n\tarray&lt;Byte&gt; ^ch = gcnew array&lt;Byte&gt;(1);\r\n\tch[0] = Convert::ToByte(SomeString[i]);\r\n\t...\r\n\tSomeString += System::Text::Encoding::UTF8-&gt;GetString(ch);\r\n<\/code><\/code><\/pre>\n<h4>\nRemoving Nulls In A String<br \/>\n<\/h4>\n<pre>\r\n<code>\r\n\tSomeString = SomeString-&gt;Replace(&quot;\\0&quot;, &quot;&quot;);\r\n<\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"<p>array&lt;Byte&gt; ^RxData = gcnew array&lt;Byte&gt;(2); RxData[0] = &#39;A&#39;; RxData[0] = &#39;B&#39;; String ^sTemp; sTemp = System::Text::Encoding::UTF8-&gt;GetString(RxData); N.B. Make the array the length of the string &#8211; do not use a 0x00 terminating byte. If you do you get a resulting string but you can&#39;t add any new characters to it (e.g. MyString += &quot;abc&quot;) because [&hellip;]<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[22],"tags":[],"class_list":["post-190","post","type-post","status-publish","format-standard","hentry","category-strings"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/190","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/comments?post=190"}],"version-history":[{"count":12,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/190\/revisions"}],"predecessor-version":[{"id":1478,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/posts\/190\/revisions\/1478"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/media?parent=190"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/categories?post=190"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/visualcpp\/wp-json\/wp\/v2\/tags?post=190"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}