<?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>Unable to convert number to uint8_t using sscanf</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/43094/unable-to-convert-number-to-uint8_t-using-sscanf</link><description>Hi, 
 
 I am trying to convert the string from ctime to a struct but i always get garbage values if i use &amp;quot;uint8_t&amp;quot;. But i can get correct values if i use &amp;quot;int&amp;quot; datatype. I want to do it using uint8_t as it will save some space, as the numbers wont be</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Wed, 30 Jan 2019 15:54:55 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/43094/unable-to-convert-number-to-uint8_t-using-sscanf" /><item><title>RE: Unable to convert number to uint8_t using sscanf</title><link>https://devzone.nordicsemi.com/thread/168757?ContentTypeID=1</link><pubDate>Wed, 30 Jan 2019 15:54:55 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:76bd1c0e-7407-4c4e-a44b-316ba7eb0781</guid><dc:creator>rgujju</dc:creator><description>&lt;p&gt;No, %hhu does not work.&amp;nbsp; What library can i use? I am using the standard library with gcc Makefile. Do i need to add some compiler flags or something?&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unable to convert number to uint8_t using sscanf</title><link>https://devzone.nordicsemi.com/thread/168559?ContentTypeID=1</link><pubDate>Tue, 29 Jan 2019 18:57:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:ba2b50fa-9e64-4369-8793-1a6cfb23da95</guid><dc:creator>hmolesworth</dc:creator><description>&lt;p&gt;I use the following for 8-bit hex; %c probably works for 8-bit character. A lot depends on the settingf for the scanf library your compiler is using; I use IAR with full library (&lt;em&gt;Use the full configuration of the C/C++ runtime library. Full locale interface, C locale, file descriptor support, multibytes in printf and scanf, and hex floats in strtod&lt;/em&gt;). If the IDE/compiler you use doesn&amp;#39;t support some things, it is also possible to use your own sscanf by searching on the web for an example source which can be edited to be minimal.&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;// 8-bit unsigned hex byte %hX:
   uint8_t AccRegisters[ACC_REGISTER_MAX_COUNT+1] = {0x40|0x38, 0x2A, 0x00, 0x25, 0x30, 0x30, 0x65};
   numValues = sscanf(p_data,&amp;quot;%hX %hX %hX %hX %hX %hX %hX&amp;quot;,
                      &amp;amp;RequestStartIndex,                // Starting register index
                      &amp;amp;AccRequestRegisters[0],           //  0
                      &amp;amp;AccRequestRegisters[1],           //  1
                      &amp;amp;AccRequestRegisters[2],           //  2
                      &amp;amp;AccRequestRegisters[3],           //  3
                      &amp;amp;AccRequestRegisters[4],           //  4
                      &amp;amp;AccRequestRegisters[5]            //  5
                         );

// 16-bit unsigned integer %hu:
   uint16_t Option;
   sscanf(p_data,&amp;quot;%hu&amp;quot;, &amp;amp;Option);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;Also &lt;em&gt;%hhu&lt;/em&gt; might work, depending on your library ..&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unable to convert number to uint8_t using sscanf</title><link>https://devzone.nordicsemi.com/thread/168551?ContentTypeID=1</link><pubDate>Tue, 29 Jan 2019 17:34:15 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:d85f5bbd-ed16-4035-ab4d-1bf2a778b476</guid><dc:creator>rgujju</dc:creator><description>&lt;p&gt;then what format specifier should i use to convert to 1 byte? I have tried with %c as well, and it doesnt seem to work.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: Unable to convert number to uint8_t using sscanf</title><link>https://devzone.nordicsemi.com/thread/168323?ContentTypeID=1</link><pubDate>Mon, 28 Jan 2019 19:19:19 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:6b18c57f-4805-4f5b-80aa-6044e2648017</guid><dc:creator>Turbo J</dc:creator><description>&lt;p&gt;The sscanf() function requires that the destination for the &amp;quot;%d&amp;quot; conversion is 4 bytes - sizeof(int) wide. This will overwrite adjacent fields in your struct of uint8_t values.&lt;/p&gt;
&lt;p&gt;Workaround: Use temporary int variables on the stack.&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>