<?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[int in const char* umwandeln]]></title><description><![CDATA[<p>Hallo ich bins wieder.<br />
Ich wollte mir 2 Funktionen schreiben, welche ohne Hilfe von anderen Headern (string) meine Integerzahl in eine Zeichenkette umwandelt. Die erste Funktion funktioniert soweit. Die Zweite nur mit Zahlenangaben von über 19. Z.B. bei dem Parameter &quot;12&quot; wandelt er diesen nicht in &quot;12&quot; um, sondern in &quot;11&quot;.</p>
<pre><code class="language-cpp">int main()
{
    const char* str2 = dynIntInStr2(12);
    cout &lt;&lt; str2 &lt;&lt; endl;

    delete [] str2;
    return 0;
}
</code></pre>
<p>Bei Zahlen ab 20 funktioniert sie. Kann es sein, dass <em>ziffer</em> abgerundet wird? Wenn ja, warum? Hier mal der Code:</p>
<pre><code class="language-cpp">char* dynIntInStr2(int z)
{
    double zCpy = z;
    size_t stellen = 0;
    while(zCpy &gt; 1.0)
    {
        zCpy /= 10;
        ++stellen;
    }

    char* temp = new char[stellen + 1];

    for(size_t i = 0; i &lt; stellen; ++i)
    {
        zCpy *= 10;
        int ziffer = zCpy;
        temp[i] = ziffer + '0';
        zCpy -= ziffer;
    }
    temp[stellen] = '\0';

    return temp;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/topic/307555/int-in-const-char-umwandeln</link><generator>RSS for Node</generator><lastBuildDate>Thu, 06 Aug 2026 20:49:52 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/307555.rss" rel="self" type="application/rss+xml"/><pubDate>Wed, 29 Aug 2012 19:18:27 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to int in const char* umwandeln on Wed, 29 Aug 2012 19:18:27 GMT]]></title><description><![CDATA[<p>Hallo ich bins wieder.<br />
Ich wollte mir 2 Funktionen schreiben, welche ohne Hilfe von anderen Headern (string) meine Integerzahl in eine Zeichenkette umwandelt. Die erste Funktion funktioniert soweit. Die Zweite nur mit Zahlenangaben von über 19. Z.B. bei dem Parameter &quot;12&quot; wandelt er diesen nicht in &quot;12&quot; um, sondern in &quot;11&quot;.</p>
<pre><code class="language-cpp">int main()
{
    const char* str2 = dynIntInStr2(12);
    cout &lt;&lt; str2 &lt;&lt; endl;

    delete [] str2;
    return 0;
}
</code></pre>
<p>Bei Zahlen ab 20 funktioniert sie. Kann es sein, dass <em>ziffer</em> abgerundet wird? Wenn ja, warum? Hier mal der Code:</p>
<pre><code class="language-cpp">char* dynIntInStr2(int z)
{
    double zCpy = z;
    size_t stellen = 0;
    while(zCpy &gt; 1.0)
    {
        zCpy /= 10;
        ++stellen;
    }

    char* temp = new char[stellen + 1];

    for(size_t i = 0; i &lt; stellen; ++i)
    {
        zCpy *= 10;
        int ziffer = zCpy;
        temp[i] = ziffer + '0';
        zCpy -= ziffer;
    }
    temp[stellen] = '\0';

    return temp;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2246808</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2246808</guid><dc:creator><![CDATA[BlauesShirt]]></dc:creator><pubDate>Wed, 29 Aug 2012 19:18:27 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Wed, 29 Aug 2012 19:39:08 GMT]]></title><description><![CDATA[<p>Die Division von Fliesskommazahlen ist nicht exakt. Probiere es mahl oohne <code>double</code> hinzubekommen und nur int zu verwenden. In Zeile 15 brauchst du dir nicht mehr die alte Zahl neu zusammenzubauen, du kannst auch gleich z verwenden und genau wie bei der Laengenbestimmung vorgehen. Auch fuer die Laengenbestimmung ist kein double erforderlich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2246813</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2246813</guid><dc:creator><![CDATA[knivil]]></dc:creator><pubDate>Wed, 29 Aug 2012 19:39:08 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Wed, 29 Aug 2012 19:43:39 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">char* dynIntInStr1(int z)
{
    int zCpy = z;
    size_t stellen = 0;
    while(zCpy != 0)
    {
        zCpy /= 10;
        ++stellen;
    }

    char* temp = new char[stellen + 1];

    size_t i = 0;
    while(z != 0)
    {
        int ziffer = z % 10;
        temp[i++] = ziffer + '0';
        z /= 10;
    }
    for(i = 0; i &lt; stellen / 2; ++i)
    {
        const char TEMP = temp[i];
        temp[i] = temp[stellen - i - 1];
        temp[stellen - i - 1] = TEMP;
    }
    temp[stellen] = '\0';

    return temp;
}
</code></pre>
<p>Wenn du das so meinst: Das ist die erste Funktion <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f603.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--grinning_face_with_big_eyes"
      title=":D"
      alt="😃"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2246815</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2246815</guid><dc:creator><![CDATA[BlauesShirt]]></dc:creator><pubDate>Wed, 29 Aug 2012 19:43:39 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Wed, 29 Aug 2012 19:51:33 GMT]]></title><description><![CDATA[<p>Simon?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2246819</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2246819</guid><dc:creator><![CDATA[dk_lol]]></dc:creator><pubDate>Wed, 29 Aug 2012 19:51:33 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Wed, 29 Aug 2012 20:27:30 GMT]]></title><description><![CDATA[<p>Ich habe vermutlich nichts zu tun.</p>
<pre><code class="language-cpp">#include &lt;cassert&gt;

char* dynIntInStr2(int z)
{
    bool sign = z &lt; 0;
    if ( !sign )
        z = -z;                       // Negation einer negativen Zahl ist u.U. undefiniert
    int tmp = z;
    unsigned digits = 0;
    do {
        ++digits;                     // eine Stelle gibt es immer (z == 0 beachten!)
    } while ( tmp /= 10 );            // Rundung in Richtung 0 ab C++11 vorgeschrieben

    unsigned length = digits + sign + 1;
    char* str = new char[length];

    str[--length] = '\0';             // Terminator
    do {
        str[--length] = '0' - z % 10; // eine Stelle gibt es immer, Rest ist &lt;= 0
    } while ( z /= 10 );

    if ( sign )
        str[--length] = '-';

    assert( length == 0 );

    return str;
}

#include &lt;iostream&gt;
#include &lt;climits&gt;

int main()
{
    const char* str2 = dynIntInStr2(INT_MIN);
    std::cout &lt;&lt; str2 &lt;&lt; '\n';;

    delete [] str2;
    return 0;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2246827</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2246827</guid><dc:creator><![CDATA[camper]]></dc:creator><pubDate>Wed, 29 Aug 2012 20:27:30 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Wed, 29 Aug 2012 20:35:38 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;cassert&gt;
#include &lt;limits&gt;
#include &lt;string&gt;

void to_string_impl(unsigned u, unsigned base, char*&amp; buf)
{
	if(u / base)
		to_string_impl(u / base, base, buf);

	*buf++ = &quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;[u % base];
}

std::string to_string(unsigned u, unsigned base)
{
	assert(base &gt;= 2 &amp;&amp; base &lt;= 36);

	char buf[std::numeric_limits&lt;unsigned&gt;::digits + 1];
	char* p = buf;
	to_string_impl(u, base, p);
	*p = 0;

	return buf;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2246828</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2246828</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Wed, 29 Aug 2012 20:35:38 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Thu, 30 Aug 2012 14:21:39 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">#include &lt;cassert&gt;
#include &lt;limits&gt;
#include &lt;string&gt;
#include &lt;algorithm&gt;

std::string to_string(unsigned long long u, unsigned long long base)
{
    assert(base &gt;= 2 &amp;&amp; base &lt;= 36);

    char buf[std::numeric_limits&lt;unsigned long long&gt;::digits + 1];
	char *p = buf;
	do
	{
		*p++ = &quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;[u % base];
	}
	while (u /= base);
	std::reverse(buf, p);
    return std::string(buf, p);
}

int main()
{
	assert(to_string(123, 10) == &quot;123&quot;);
	assert(to_string(13, 2) == &quot;1101&quot;);

	for (unsigned i = 2; i &lt;= 36; ++i)
	{
		assert(to_string(0, i) == &quot;0&quot;);
	}
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2246846</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2246846</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Thu, 30 Aug 2012 14:21:39 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Thu, 30 Aug 2012 14:14:40 GMT]]></title><description><![CDATA[<p>TyRoXx schrieb:</p>
<blockquote>
<pre><code class="language-cpp">std::string to_string(unsigned long long u, unsigned long long base)
{
    assert(base &gt;= 2 &amp;&amp; base &lt;= 36);
	
    char buf[std::numeric_limits&lt;unsigned long long&gt;::digits + 1];
	char *p = buf;
	do
	{
		*p++ = &quot;0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ&quot;[u % base];
	}
	while (u /= base);
	std::reverse(buf, p);
    return std::string(buf, p);
}
</code></pre>
</blockquote>
<p>Statt assert sollte man aber throw verwenden. assert ist nicht im C++ standard enthalten. Außerdem kann throw abgefangen werden, sodass ein kleiner Fehler nicht gleich den Absturz des Programmes bedeutet.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2247025</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247025</guid><dc:creator><![CDATA[Marthog]]></dc:creator><pubDate>Thu, 30 Aug 2012 14:14:40 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Thu, 30 Aug 2012 14:32:36 GMT]]></title><description><![CDATA[<p>Marthog schrieb:</p>
<blockquote>
<p>Statt assert sollte man aber throw verwenden.</p>
</blockquote>
<p>In C und C++ nimmt man für Fehler, die der Programmierer beim Benutzen einer Funktion macht, <code>assert</code> .<br />
<code>throw</code> ist für Laufzeitfehler da.</p>
<p>Marthog schrieb:</p>
<blockquote>
<p>assert ist nicht im C++ standard enthalten.</p>
</blockquote>
<p>Doch, ist es.</p>
<p>Marthog schrieb:</p>
<blockquote>
<p>Außerdem kann throw abgefangen werden, sodass ein kleiner Fehler nicht gleich den Absturz des Programmes bedeutet.</p>
</blockquote>
<p>Man <em>möchte</em> aber, dass das Programm beendet wird, denn es ist fehlerhaft. Wer weiß, was es kaputt macht, wenn man Programmierfehler ignoriert.<br />
Außerdem können die meisten nicht Ausnahmen-sicher programmieren oder wollen es nicht, sodass auch noch Ressourcen &quot;lecken&quot; würden.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2247028</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247028</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Thu, 30 Aug 2012 14:32:36 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Thu, 30 Aug 2012 14:30:34 GMT]]></title><description><![CDATA[<p>Marthog schrieb:</p>
<blockquote>
<p>assert ist nicht im C++ standard enthalten.</p>
</blockquote>
<p>Siehe N3337, 19.3 Klausel 1/2.</p>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>Marthog schrieb:</p>
<blockquote>
<p>Statt assert sollte man aber throw verwenden.</p>
</blockquote>
<p>In C und C++ nimmt man für solche Fehler <code>assert</code> . <code>throw</code> ist für Laufzeitfehler da.</p>
</blockquote>
<p>Hier widersprichst du dir selbst, weil jeder Fehler den du mit assert prüfst, erst zur Laufzeit als solcher erkannt werden kann und damit auch ein Laufzeitfehler ist.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2247032</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247032</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 30 Aug 2012 14:30:34 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Thu, 30 Aug 2012 14:38:12 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>TyRoXx schrieb:</p>
<blockquote>
<p>Marthog schrieb:</p>
<blockquote>
<p>Statt assert sollte man aber throw verwenden.</p>
</blockquote>
<p>In C und C++ nimmt man für solche Fehler <code>assert</code> . <code>throw</code> ist für Laufzeitfehler da.</p>
</blockquote>
<p>Hier widersprichst du dir selbst, weil jeder Fehler den du mit assert prüfst, erst zur Laufzeit als solcher erkannt werden kann und damit auch ein Laufzeitfehler ist.</p>
</blockquote>
<p>Ich habe es noch einmal klarer formuliert.<br />
Ein Laufzeitfehler ist einer, der nicht durch die Analyse des Quellcodes erkannt werden kann: &quot;Datei nicht gefunden&quot;, &quot;Speicher alle&quot; etc.<br />
Solche Fehler gibt man typischerweise mit <code>throw</code> weiter, damit sie behandelt werden können.<br />
Programmierfehler sollen höchstens vom Debugger behandelt werden, also ist da <code>assert</code> optimal.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2247035</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247035</guid><dc:creator><![CDATA[TyRoXx]]></dc:creator><pubDate>Thu, 30 Aug 2012 14:38:12 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Thu, 30 Aug 2012 15:03:06 GMT]]></title><description><![CDATA[<p>Marthog schrieb:</p>
<blockquote>
<p>sodass ein kleiner Fehler nicht gleich den Absturz des Programmes bedeutet.</p>
</blockquote>
<p>Es ist kein Kleiner Fehler. Der Programmierer hat darauf zu achten, den Anforderungen der Funktion gerecht zu werden (gerade bei Assertions sollte man vorher einen Range-Check durchführen).</p>
<p>Wie Tyrrox auch gesagt hat, generiert <code>assert()</code> ein SIGABRT Signal, was wiederum leicht genutzt werden kann, um bspw. Auf die Ursache zu schließen (mit dem Stackframe).</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2247047</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247047</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 30 Aug 2012 15:03:06 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Thu, 30 Aug 2012 15:04:49 GMT]]></title><description><![CDATA[<p>Übrigens würde ich hier tatsächlich eher werfen. Ein <code>out_of_range</code> wäre doch eigentlich angebracht, oder nicht? <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f642.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--slightly_smiling_face"
      title=":)"
      alt="🙂"
    /><br />
Die Aussage, dass man in C++ kein <code>assert()</code> benutzt, ist natürlich aber weiterhin Quatsch.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2247048</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247048</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Thu, 30 Aug 2012 15:04:49 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Thu, 30 Aug 2012 15:20:44 GMT]]></title><description><![CDATA[<p>assert tut im Release-Mode einfach gar nichts. -&gt; assert</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2247051</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247051</guid><dc:creator><![CDATA[Kellerautomat]]></dc:creator><pubDate>Thu, 30 Aug 2012 15:20:44 GMT</pubDate></item><item><title><![CDATA[Reply to int in const char* umwandeln on Thu, 30 Aug 2012 15:23:51 GMT]]></title><description><![CDATA[<p>Sone schrieb:</p>
<blockquote>
<p>Übrigens würde ich hier tatsächlich eher werfen.</p>
</blockquote>
<p>out of range ist ein logikfehler im programm. hier ist assert der richtige weg.</p>
<p>logikfehler mit exceptions zu behandeln verleiten dazu sie zu ignorieren. das ist immer schlecht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2247052</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2247052</guid><dc:creator><![CDATA[Shade Of Mine]]></dc:creator><pubDate>Thu, 30 Aug 2012 15:23:51 GMT</pubDate></item></channel></rss>