<?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>sprintf format issue</title><link>https://devzone.nordicsemi.com/f/nordic-q-a/79829/sprintf-format-issue</link><description>Hi, 
 I am doing a simple thing with string formatting: 
 sprintf(tmp_str,&amp;quot;%d.%d.%3d&amp;quot;, version.major, version.minor, version.patch); 
 The version structure has values { 0, 5, 1 } and char tmp_str[20]. I want to achieve string like &amp;quot;0.5.001&amp;quot;. Instead</description><dc:language>en-US</dc:language><generator>Telligent Community 13</generator><lastBuildDate>Tue, 28 Sep 2021 08:53:27 GMT</lastBuildDate><atom:link rel="self" type="application/rss+xml" href="https://devzone.nordicsemi.com/f/nordic-q-a/79829/sprintf-format-issue" /><item><title>RE: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/331449?ContentTypeID=1</link><pubDate>Tue, 28 Sep 2021 08:53:27 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f7c3f9cb-4ef4-4c78-84fb-252c70011b09</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Ted&lt;/p&gt;
&lt;p&gt;&amp;quot;%03d&amp;quot; won&amp;#39;t work, but &amp;quot;%.3d&amp;quot; worked fine in my testing at least.&amp;nbsp;&lt;/p&gt;
&lt;p&gt;I have used similar syntax many times earlier in order to display 32-bit&amp;nbsp;registers as padded hex values:&amp;nbsp;&lt;/p&gt;
&lt;p&gt;// Provide hexadecimal formatting.&amp;nbsp;Example,&amp;nbsp;2 is displayed as 0x00000002&lt;br /&gt;&amp;quot;0x%.8x&amp;quot;&lt;/p&gt;
&lt;p&gt;Regardless, I wouldn&amp;#39;t be surprised if this can vary a bit with the implementation of the standard libraries.&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: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/331355?ContentTypeID=1</link><pubDate>Mon, 27 Sep 2021 14:31:50 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2d3f819d-d75e-417c-ad08-dfc130d4121f</guid><dc:creator>tedhavelka</dc:creator><description>&lt;p&gt;Hi Tob&lt;span&gt;j&amp;oslash;rn,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;I believe the leading zero in &amp;quot;%03d&amp;quot; is necessary to get output that has as many leading zeroes as needed to create fixed width tokens. But this should work with numeric and string tokens.&amp;nbsp; No reason &amp;quot;%03s&amp;quot; or &amp;quot;%010s&amp;quot; or similar would output differently in terms of width.&lt;/p&gt;
&lt;p&gt;Regarding this language detail, I came across this subtle C formatting syntax several years ago when I wanted fix column output.&amp;nbsp; In particular leading zeroes provided me text fields which would alphabetize at a Unix shell prompt.&amp;nbsp; I am reluctant to assume &amp;quot;%3d&amp;quot; will product desired output without testing on the same target hardware which Peter has.&amp;nbsp; Testing on my side shows that &amp;quot;%3d&amp;quot; does not pad output with leading zeroes on a typical Ubuntu host running a modified, libc6 based &amp;quot;hello world&amp;quot; program.&lt;br /&gt;&lt;br /&gt;And so too I would be assuming at this point that the leading zero in &amp;quot;%03d&amp;quot; can be any printable character, e.g. &amp;quot;%z3d&amp;quot; would pad with leading &amp;quot;z&amp;quot; characters but I must test this to be sure.&amp;nbsp; Somewhere too there is documentation among libc6 materials on this less often used feature of printf() family formatters.&lt;/p&gt;
&lt;p&gt;- Ted&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/331256?ContentTypeID=1</link><pubDate>Mon, 27 Sep 2021 10:00:49 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2125e9dd-be68-49c2-aa7d-4576ed3c6cb1</guid><dc:creator>Peter.J</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;&amp;quot;%d.%d%.3d&amp;quot; gives&amp;nbsp;me &amp;quot;v0.5001&amp;quot;.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Thanx again to both of you for the support.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;peter&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/331253?ContentTypeID=1</link><pubDate>Mon, 27 Sep 2021 09:51:22 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b8af74b0-2fd7-4c53-9b0e-448e7e78ceb4</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi&amp;nbsp; Peter&lt;/p&gt;
&lt;p&gt;I assume&amp;nbsp;&lt;span&gt;&amp;quot;%d.%d.%.3d&amp;quot; would also work?&amp;nbsp;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Anyway,&amp;nbsp;excellent to hear that you were able to get it working &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;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards&lt;br /&gt;Torbjørn&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/331179?ContentTypeID=1</link><pubDate>Sun, 26 Sep 2021 09:19:30 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:01ee7b3f-cfc4-48dd-ac86-24ef18e2661c</guid><dc:creator>Peter.J</dc:creator><description>&lt;p&gt;Hi Ted,&lt;/p&gt;
&lt;p&gt;I realized I need to use the following string format:&amp;nbsp;&amp;quot;%d.%d.%03d&amp;quot;. Then I am getting the output as&amp;nbsp;expected: &amp;quot;0.5.001&amp;quot;.&lt;/p&gt;
&lt;p&gt;Thanx a lot, Ted and&amp;nbsp;&lt;span&gt;Torbj&amp;oslash;rn, for an excellent support.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;peter&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/331170?ContentTypeID=1</link><pubDate>Sat, 25 Sep 2021 10:39:37 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:385cce02-b54f-4790-8727-08feb4a76fac</guid><dc:creator>Peter.J</dc:creator><description>&lt;p&gt;Hi Ted,&lt;/p&gt;
&lt;p&gt;thanx for your suggestion. We moved a bit, still not at the goal.&lt;/p&gt;
&lt;p&gt;I had the printf precision off so I changed it to yes. The result is as follows:&lt;/p&gt;
&lt;p&gt;- when using string format&amp;nbsp;&amp;quot;%d.%d.%3d&amp;quot; the output is &amp;quot;v0.5.&amp;nbsp; 1&amp;quot;&lt;/p&gt;
&lt;p&gt;- when using string format &amp;quot;%d.%d%.3d&amp;quot;, the output is &amp;quot;v0.5001&amp;quot;&lt;/p&gt;
&lt;p&gt;A bit better but still not what we would like to see :)&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;peter&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/331167?ContentTypeID=1</link><pubDate>Sat, 25 Sep 2021 07:24:43 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:f1fd734d-b59b-4824-af58-bca9f7086d21</guid><dc:creator>tedhavelka</dc:creator><description>&lt;p&gt;Hello Peter and Torbj&amp;oslash;rn,&lt;/p&gt;
&lt;p&gt;Peter I realize a few days have passed since your initial post.&amp;nbsp; Have you been able to solve your printf() formatting issue yet?&amp;nbsp; Today while working on a different project and problem I came across a new project dialog in Nordic&amp;#39;s version of Segger (Nordic SES release 5.6), with extra support for nRF Connect SDK projects.&amp;nbsp; A &amp;quot;Common Project Settings&amp;quot; dialog there catches my eye.&amp;nbsp; There are references to several printf functionalities, most of which are disabled by default.&lt;/p&gt;
&lt;p&gt;One of the options is called &amp;quot;printf width/precision&amp;quot;.&amp;nbsp; Here is a window capture of this dialog box:&lt;img alt="SES &amp;quot;Choose Common Project Settings&amp;quot;" src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/Screenshot_2D002D00_2021_2D00_09_2D00_24_2D002D00_ses_2D002D00_choose_2D00_common_2D00_project_2D00_settings.png" /&gt;&lt;/p&gt;
&lt;p&gt;When I initially wrote and suggested to try &amp;quot;%03d&amp;quot; format specifier in place of &amp;quot;%3d&amp;quot; specifier, I first wrote a short C program to be sure this specifier worked.&amp;nbsp; Running the program on the host workstation confirmed this.&amp;nbsp; The version of C which I was able to test against is:&lt;/p&gt;
&lt;p&gt;&lt;span style="font-family:comic sans ms, comic sans, sans-serif;"&gt;$ /lib/x86_64-linux-gnu/libc.so.6&lt;/span&gt;&lt;br /&gt;&lt;span style="font-family:comic sans ms, comic sans, sans-serif;"&gt;GNU C Library (Ubuntu GLIBC 2.31-0ubuntu9.2) stable release version 2.31.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;Compiling C natively under Ubuntu 20.04, libc6 printf libraries are fully enabled by default.&amp;nbsp; Printf family functions however, as you likely know, can be too large for microcontrollers with limited flash and other memory, so significant parts (or all) of printf formatting are often disabled by default in firmware development settings.&lt;/p&gt;
&lt;p&gt;Perhaps in your project environment some of these printf functionalities are disabled right now, like the ones in the window capture above?&lt;/p&gt;
&lt;p&gt;- Ted&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/331016?ContentTypeID=1</link><pubDate>Fri, 24 Sep 2021 09:37:31 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:39aee7f2-14ff-4f3b-a9b3-a45732f72966</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Peter&lt;/p&gt;
&lt;p&gt;Then&amp;nbsp;I am running low on ideas, it works fine here ;)&lt;/p&gt;
&lt;p&gt;Are you able to zip your project folder (including project and source files) and share it with me, so I can test out your exact configuration?&lt;/p&gt;
&lt;p&gt;You might want to remove the build files first, to reduce the file size.&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: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/330795?ContentTypeID=1</link><pubDate>Thu, 23 Sep 2021 10:23:41 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:b97eea43-cc44-4d03-a8fc-c14df74cea99</guid><dc:creator>Peter.J</dc:creator><description>&lt;p&gt;Hi&amp;nbsp;Torbj&amp;oslash;rn,&lt;/p&gt;
&lt;p&gt;the variables are of int type.&lt;/p&gt;
&lt;p&gt;I checked the tmp_str values just after calling the sprintf(). You can see the values on the screenshot:&lt;/p&gt;
&lt;p&gt;&lt;img src="https://devzone.nordicsemi.com/resized-image/__size/320x240/__key/communityserver-discussions-components-files/4/pastedimage1632392542043v1.png" alt=" " /&gt;&lt;/p&gt;
&lt;p&gt;Best regards,&lt;/p&gt;
&lt;p&gt;peter&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/330793?ContentTypeID=1</link><pubDate>Thu, 23 Sep 2021 10:14:46 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:74290f23-6cc1-4cbc-b05e-daabc99fc238</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Peter&lt;/p&gt;
&lt;p&gt;What is the type of the version.major, version.minor and version.patch fields?&lt;/p&gt;
&lt;p&gt;Could you try to cast them to int in the call to sprintf?&lt;/p&gt;
&lt;p&gt;sprintf(tmp_str,&amp;quot;%d.%d%.3d&amp;quot;, (int)version.major, &lt;span&gt;(int)&lt;/span&gt;version.minor, &lt;span&gt;(int)&lt;/span&gt;version.patch);&lt;/p&gt;
&lt;p&gt;If it&amp;#39;s still not working, could you check the value of the tmp_str buffer in the debugger?&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: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/330648?ContentTypeID=1</link><pubDate>Wed, 22 Sep 2021 12:03:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:1f6c7cbc-eb95-4256-80a8-c2717cb6298a</guid><dc:creator>Peter.J</dc:creator><description>&lt;p&gt;&lt;span&gt;Hi Torbj&amp;oslash;rn,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;I am based on ble_peripheral, ble_app_template.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;The version string I am cheking through DIS service. I now tried to use NRF_LOG_INFO this way (the structure values are 0, 5, 1):&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;pre class="ui-code" data-mode="c_cpp"&gt;sprintf(tmp_str,&amp;quot;%d.%d%.3d&amp;quot;, version.major, version.minor, version.patch);

NRF_LOG_INFO(&amp;quot;Firmware version: v%s&amp;quot;, tmp_str);

printf(&amp;quot;Firmware version: v%s&amp;quot;, tmp_str);
&lt;/pre&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;/p&gt;
&lt;p&gt;I am getting output on the console:&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&amp;lt;info&amp;gt; app: Firmware version: v&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;There is no version number in the output form NRF_LOG_INFO and nothing from the printf().&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;peter&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/330626?ContentTypeID=1</link><pubDate>Wed, 22 Sep 2021 11:10:39 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:c9f153a9-ffc3-4fa6-80ce-0264998c8fab</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Peter&lt;/p&gt;
&lt;p&gt;The following code seems to work fine for me:&lt;/p&gt;
&lt;p&gt;&lt;pre class="ui-code" data-mode="text"&gt;int a = 0;
int b = 5;
int c = 1;
uint8_t testbuf[65];
sprintf(testbuf, &amp;quot;Test %d.%d.%.3d&amp;quot;, a, b, c);
printf(&amp;quot;%s\r\n&amp;quot;, testbuf);&lt;/pre&gt;&lt;/p&gt;
&lt;p&gt;The printed output is this:&lt;/p&gt;
&lt;p&gt;&lt;em&gt;Test 0.5.001&lt;/em&gt;&lt;/p&gt;
&lt;p&gt;Could you try to run the same code?&lt;/p&gt;
&lt;p&gt;If it still doesn&amp;#39;t work, can you let me know which of the projects in the SDK you are using?&lt;br /&gt;I based my test on the ble_app_uart example.&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: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/330433?ContentTypeID=1</link><pubDate>Tue, 21 Sep 2021 09:58:57 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:784e9bb3-5549-4601-aa79-0a9c80f4ea6d</guid><dc:creator>Peter.J</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 tried&amp;nbsp;&amp;quot;%d.%d%.3d&amp;quot; and it gives me 0.53d output.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;peter&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/330424?ContentTypeID=1</link><pubDate>Tue, 21 Sep 2021 09:52:10 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:576590bb-2b6e-4d20-8e96-ffad5c468080</guid><dc:creator>ovrebekk</dc:creator><description>&lt;p&gt;Hi Peter&lt;/p&gt;
&lt;p&gt;Can you try with %.3d instead?&lt;/p&gt;
&lt;p&gt;That should pad the number with 0&amp;#39;s up to three digits.&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: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/330349?ContentTypeID=1</link><pubDate>Mon, 20 Sep 2021 18:17:08 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:2f0cfae1-1dd4-4a31-8a97-9769f97359f2</guid><dc:creator>Peter.J</dc:creator><description>&lt;p&gt;Hi Ted,&lt;/p&gt;
&lt;p&gt;thanx for your feedback. Using&amp;nbsp;&lt;span&gt;&amp;quot;%d.%d.%03d&amp;quot; doesnt make a difference, the output is 0.5.3d.&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;Best regards,&lt;/span&gt;&lt;/p&gt;
&lt;p&gt;&lt;span&gt;peter&lt;/span&gt;&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item><item><title>RE: sprintf format issue</title><link>https://devzone.nordicsemi.com/thread/330346?ContentTypeID=1</link><pubDate>Mon, 20 Sep 2021 17:19:36 GMT</pubDate><guid isPermaLink="false">137ad170-7792-4731-bb38-c0d22fbe4515:e51993cd-88b4-4e84-8aaf-be8b76e008d2</guid><dc:creator>tedhavelka</dc:creator><description>&lt;p&gt;Hi Peter,&lt;/p&gt;
&lt;p&gt;If you amend the beginning of your first line of code `sprintf(tmp_str,&amp;quot;%d.%d.%3d&amp;quot; . . .` such that the format string reads `&amp;quot;%d.%d.%03d&amp;quot;` does this give the formatting you seek?&lt;/p&gt;
&lt;p&gt;- Ted&lt;/p&gt;&lt;div style="clear:both;"&gt;&lt;/div&gt;</description></item></channel></rss>