<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Convert DS18B20 temperature to string]]></title><description><![CDATA[<p dir="auto">I want to convert DS18B20( [link removed by moderator]: )<br />
temperature (4-bytes code from datasheet) to string with accuracy 0.1°C (like sprintf %.1f). AVR. C language (avr-gcc). I need for small code, so sprintf, floating-point types and round from math.h is bad idea. My following current code is bad also:</p>
<p dir="auto">void reverse_string(char *s)<br />
{<br />
char *p, c;</p>
<pre><code>for (p = s + strlen(s) - 1; s &lt;= p; ++s, --p) {
    c = *p;
    *p = *s;
    *s = c;
}
</code></pre>
<p dir="auto">}</p>
<p dir="auto">void ts_to_string(uint16_t ts, char *s)<br />
{<br />
int8_t n = (int8_t)(ts &gt;&gt; 4);<br />
uint8_t neg = n &amp; 0x80;<br />
char *p = s;<br />
float f;</p>
<pre><code>if (neg)
    n = -n-1;
do {
    *p++ = n % 10 + '0';
} while ((n /= 10) &gt; 0);

if (neg)
    *p++ = '-';

*p = '\0';
reverse_string(s);
*p++ = '.';

f = (float)(ts &amp; 0xf) / 16.0;
if (neg)
    f = 1.0-f;
*p++ = (char)round(f * 10.0) + '0';
*p = '\0';
</code></pre>
]]></description><link>https://forum.mysensors.org/topic/7049/convert-ds18b20-temperature-to-string</link><generator>RSS for Node</generator><lastBuildDate>Fri, 10 Jul 2026 11:46:00 GMT</lastBuildDate><atom:link href="https://forum.mysensors.org/topic/7049.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 23 Jun 2017 07:36:32 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Convert DS18B20 temperature to string on Fri, 23 Jun 2017 07:48:37 GMT]]></title><description><![CDATA[<p dir="auto">Accound banned for spam. Promotes a vendor and has copied a question from <a href="https://electronics.stackexchange.com/questions/33103/convert-ds18b20-temperature-to-string" rel="nofollow ugc">https://electronics.stackexchange.com/questions/33103/convert-ds18b20-temperature-to-string</a></p>
]]></description><link>https://forum.mysensors.org/post/69716</link><guid isPermaLink="true">https://forum.mysensors.org/post/69716</guid><dc:creator><![CDATA[mfalkvidd]]></dc:creator><pubDate>Fri, 23 Jun 2017 07:48:37 GMT</pubDate></item></channel></rss>