<?xml version="1.0" encoding="UTF-8" ?>
<?xml-stylesheet type="text/xsl" href="https://devzone.nordicsemi.com/cfs-file/__key/system/syndication/rss.xsl" media="screen"?><rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:slash="http://purl.org/rss/1.0/modules/slash/" xmlns:wfw="http://wellformedweb.org/CommentAPI/" xmlns:atom="http://www.w3.org/2005/Atom"><channel><title>Not able to convert hex to float using the float_t typecast.Is there a workaround for this?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/55663/not-able-to-convert-hex-to-float-using-the-float_t-typecast-is-there-a-workaround-for-this</link><description>So for this what I am trying to do is- 
 result = ((float_t)0x3F4040A3); NRF_LOG_INFO(&amp;quot;My float number&amp;quot; NRF_LOG_FLOAT_MARKER &amp;quot;\r\n&amp;quot;, NRF_LOG_FLOAT(result)); 
 But this is giving me result to be as ‭1061175459‬.00 rather than 0.750986. 
 So for this conversion</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 18 Dec 2019 04:54:19 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/55663/not-able-to-convert-hex-to-float-using-the-float_t-typecast-is-there-a-workaround-for-this" /><item><title>RE: Not able to convert hex to float using the float_t typecast.Is there a workaround for this?</title><link>https://devzone.nordicsemi.com/thread/226005?ContentTypeID=1</link><pubDate>Wed, 18 Dec 2019 04:54:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e44a28d4-4fb0-424b-a941-b45212e82044</guid><dc:creator>Gaurav</dc:creator><description>&lt;p&gt;Thank You so much for the help.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Not able to convert hex to float using the float_t typecast.Is there a workaround for this?</title><link>https://devzone.nordicsemi.com/thread/225669?ContentTypeID=1</link><pubDate>Mon, 16 Dec 2019 13:47:02 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e081d53c-0d03-455f-b88a-713a55efffd4</guid><dc:creator>tesc</dc:creator><description>&lt;p&gt;Hi,&lt;/p&gt;
&lt;p&gt;Typecasting 0x3F4040A3 (which is an integer) to the type float_t will not treat the binary data from the hexadecimal notation and treat that as if it were a float.&lt;/p&gt;
&lt;p&gt;Converting the data from the integer 0x3F4040A3, which is 1061175459 in decimal, to a float should yield the bytes 0x4e7d0103, which is 1061175488.00 in decimal notation (with small error due to conversion / lack of precision.)&lt;/p&gt;
&lt;p&gt;To add to the confusion, the size of float_t may be bigger than that of float. float_t is implementation specific and may differ from toolchain to toolchain or even from one version of a toolchain to the next. What I am saying is you might want to use float instead of float_t. The fact that you did not seem to lose precision probably means your float_t is not 32 bits (4 bytes) but rather longer than that. You can check the data size (in bytes) for the float_t type with &amp;quot;sizeof(float_t)&amp;quot;.&lt;/p&gt;
&lt;p&gt;If you have a constant, defined as a hexadecimal value, that should be treated as a float, if it is known at compile time it is better to convert it manually and store it directly as a float (e.g. &amp;quot;result = 0.750986&amp;quot;.)&lt;/p&gt;
&lt;p&gt;If you really need to treat the hexadecimal value as if it were the (binary) representation of a float, then storing it in an int, then casting a pointer to that int from an int pointer to a float pointer, then reading the value pointed to by that float pointer, might be a way to do this. Or, alternatively, use a union of float and int, then store the int and read the float.&lt;/p&gt;
&lt;p&gt;Regards,&lt;br /&gt;Terje&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>