{"id":662,"date":"2015-09-28T12:46:09","date_gmt":"2015-09-28T12:46:09","guid":{"rendered":"https:\/\/ibex.tech\/c\/?p=662"},"modified":"2024-06-07T16:40:59","modified_gmt":"2024-06-07T15:40:59","slug":"converting-variables","status":"publish","type":"post","link":"https:\/\/ibex.tech\/c\/c\/memory\/converting-variables","title":{"rendered":"Converting Variables"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\">Convert float to bytes<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>union {\n\tfloat f;\n\tuint8_t ui8&#91;4];\n\t\/\/uint32_t ui32;\t\t\t\/\/&lt;&lt;Could also use if you wanted\n} MyFloatBytesUnionValue;\n\n\tMyFloatBytesUnionValue.f = 1.234;\n\tfor (Index = 0; Index &lt; 4; Index++) \n\t\tprintf (\"byte %d is %02x\\n\", Index, MyFloatBytesUnionValue.ui8&#91;Index]);<\/code><\/pre>\n\n\n\n<h4 class=\"wp-block-heading\">Convert bytes to float<\/h4>\n\n\n\n<pre class=\"wp-block-code\"><code>union {\n\tfloat f;\n\tuint8_t ui8&#91;4];\n\t\/\/uint32_t ui32;\t\t\t\/\/&lt;&lt;Could also use if you wanted\n} MyFloatBytesUnionValue;\n\n\tMyFloatBytesUnionValue.ui8&#91;3] = 0x1f;\n\tMyFloatBytesUnionValue.ui8&#91;2] = 0x85;\n\tMyFloatBytesUnionValue.ui8&#91;1] = 0x45;\n\tMyFloatBytesUnionValue.ui8&#91;0] = 0x41;\n\n\t\/\/Or you could do this:\n\t\/\/MyFloatBytesUnionValue.ui32 = ((uint32_t)byte_value&#91;3] &lt;&lt; 24) | ((uint32_t)byte_value&#91;2] &lt;&lt; 16) | ((uint32_t)byte_value&#91;1] &lt;&lt; 8) | (uint32_t)byte_value&#91;0];\n\n\tprintf (\"float is %f\\n\", MyFloatBytesUnionValue.f);<\/code><\/pre>\n\n\n\n<p><em>Note, if this appears not to work (you get zero out but with a valid&nbsp;float value in) then try reversing the byte order<\/em>!<\/p>\n\n\n\n<h5 class=\"wp-block-heading\">Example<\/h5>\n\n\n\n<p>The following input bytes 0x1f, 0x85, 0x45, 0x41 will produce a float output of 12.345<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Convert float to bytes Convert bytes to float Note, if this appears not to work (you get zero out but with a valid&nbsp;float value in) then try reversing the byte order! Example The following input bytes 0x1f, 0x85, 0x45, 0x41 will produce a float output of 12.345<\/p>\n","protected":false},"author":5,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[9],"tags":[],"class_list":["post-662","post","type-post","status-publish","format-standard","hentry","category-memory"],"_links":{"self":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/662","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/users\/5"}],"replies":[{"embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/comments?post=662"}],"version-history":[{"count":5,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/662\/revisions"}],"predecessor-version":[{"id":1587,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/posts\/662\/revisions\/1587"}],"wp:attachment":[{"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/media?parent=662"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/categories?post=662"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ibex.tech\/c\/wp-json\/wp\/v2\/tags?post=662"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}