<?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>How do I change data type float to hex?</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/91198/how-do-i-change-data-type-float-to-hex</link><description>Hi, I use nRF52840, with SDK 15.3.0 
 Using UART communication, stored data should be unsigned integer 16 bit. 
 And the data should be calculated to float data type. 
 
 To summarize, declare data type to unsigned integer, calculate to float type, change</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 30 Aug 2022 17:34:03 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/91198/how-do-i-change-data-type-float-to-hex" /><item><title>RE: How do I change data type float to hex?</title><link>https://devzone.nordicsemi.com/thread/383987?ContentTypeID=1</link><pubDate>Tue, 30 Aug 2022 17:34:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:44171beb-fd69-4bd6-8de2-6b25a64238d9</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi William&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Good to hear you found a way to do it &lt;span class="emoticon" data-url="https://devzone.nordicsemi.com/cfs-file/__key/system/emoji/1f642.svg" title="Slight smile"&gt;&amp;#x1f642;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do I change data type float to hex?</title><link>https://devzone.nordicsemi.com/thread/383817?ContentTypeID=1</link><pubDate>Tue, 30 Aug 2022 08:30:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:3a652aa8-924e-4fe7-ad81-bc032d268fb6</guid><dc:creator>William_dev</dc:creator><description>&lt;p&gt;Hi,&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I Use Segger Embedded studio IDE.&lt;/p&gt;
&lt;p&gt;I have done and solve the problem.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;float value = 0.3f;

volatile uint16_t mtr[2] = {0, };

int main()
{
    *(float*)mtr = value;
    //mtr[1] = 0x3E99, mtr[0] = 0x999A
}&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;William&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do I change data type float to hex?</title><link>https://devzone.nordicsemi.com/thread/383814?ContentTypeID=1</link><pubDate>Tue, 30 Aug 2022 08:22:03 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:a8d2f071-b261-430f-bc38-0a9c80654c60</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Which IDE/compiler are you using?&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I&amp;nbsp;modified one of the standard SDK examples to run the code I shared, and it build without problems:&lt;br /&gt;&lt;a href="https://devzone.nordicsemi.com/cfs-file/__key/communityserver-discussions-components-files/4/294158_5F00_float_5F00_test.zip"&gt;devzone.nordicsemi.com/.../294158_5F00_float_5F00_test.zip&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;I tested it with 0.3 assigned to the float, and got the following log output:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;&amp;lt;info&amp;gt; app: Float conversion test&lt;/em&gt;&lt;br /&gt;&lt;em&gt;&amp;lt;info&amp;gt; app: 999A - 3E99&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;I am using Segger Embedded Studio, and SDK v17.1.0 for this test, but I would expect the code to run fine in other compilers also.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do I change data type float to hex?</title><link>https://devzone.nordicsemi.com/thread/383625?ContentTypeID=1</link><pubDate>Mon, 29 Aug 2022 09:06:12 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6675eb51-0046-446b-9814-a7f5d3267ad4</guid><dc:creator>William_dev</dc:creator><description>&lt;p&gt;Let me show you the neighboring example below.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;float value = 0.3; // 0x3e99999a

volatile uint16_t mtr[2] = {0, };

/* Value should be change to mtr[1], mtr[0]  */
mtr[1] = 0x3e99;
mtr[0] = 0x999a;&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;How can I make float value to 16 bit hex value?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do I change data type float to hex?</title><link>https://devzone.nordicsemi.com/thread/383586?ContentTypeID=1</link><pubDate>Mon, 29 Aug 2022 07:11:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6397ce9c-5eaf-4065-b4a9-74e0d1f2882a</guid><dc:creator>William_dev</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;span&gt;Torbj&amp;oslash;rn,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I did your recommend code, but it work some errors.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;1. cannot convert to a pointer type&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;2.&amp;nbsp;invalid initializer&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;3.&amp;nbsp;data definition has no type or storage class [-Werror]&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;4.&amp;nbsp;cannot convert to a pointer type&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do I change data type float to hex?</title><link>https://devzone.nordicsemi.com/thread/383085?ContentTypeID=1</link><pubDate>Wed, 24 Aug 2022 13:31:23 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:9038ac6d-faac-4b92-876d-f1be5f6970c1</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi William&lt;/p&gt;
&lt;p&gt;In that case you should be able to copy the raw value of the upper and lower bytes of your float directly into your two uint16 variables.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;A trick to do this is to get a pointer to your float, cast it to a uint16 pointer, and then dereference the pointer again to only access the lower and upper 16 bits.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;Please try the following code:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;float measure_value;
uint16_t buffer[2];
buffer[0] = ((uint16_t*)&amp;amp;measure_value)[0];
buffer[1] = ((uint16_t*)&amp;amp;measure_value)[1];&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do I change data type float to hex?</title><link>https://devzone.nordicsemi.com/thread/383008?ContentTypeID=1</link><pubDate>Wed, 24 Aug 2022 08:11:25 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f485ea9b-1987-4a90-a8c0-6f8a325e385f</guid><dc:creator>William_dev</dc:creator><description>&lt;p&gt;Thank you for your quick reply.&lt;/p&gt;
&lt;p&gt;I&amp;#39;d like to store value 0.0~12.7 convert to unsigned integer 16-bit, and the value will be express to two size of uint16_t array.&lt;/p&gt;
&lt;p&gt;ex) uint16_t buffer[2] = {0x0000, 0x0000};&lt;/p&gt;
&lt;p&gt;&amp;nbsp; buffer[0] = measure_value(converted to hex);&lt;/p&gt;
&lt;p&gt;&amp;nbsp; buffer[1] = measure_value(converted to hex)&amp;lt;&amp;lt;16;&lt;/p&gt;
&lt;p&gt;Best regards&lt;/p&gt;
&lt;p&gt;William&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: How do I change data type float to hex?</title><link>https://devzone.nordicsemi.com/thread/382883?ContentTypeID=1</link><pubDate>Tue, 23 Aug 2022 10:40:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c9f5cb24-4dda-4cb2-a0ce-b11514561f32</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;&lt;/p&gt;
&lt;p&gt;As you probably know a float data type in C is stored in 32 bits, so if you want to convert a float value to 16 bits you will need to accept some loss of precision.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;What kind of numbers do you need to store? Will they not contain a fractional part, only integer values below 2^16 (to fit in a unsigned 16 bit integer) ?&lt;/p&gt;
&lt;p&gt;Best regards&lt;br /&gt;Torbjørn&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>