<?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[new char[i] immer 10 chars lang]]></title><description><![CDATA[<p>Wieder eine ganz einfache Frage... hoffentlich hat sie auch eine einfach Lösung --&gt; warum ist der neue char[] immer 10 chars groß?<br />
Kompiliere mit dem MSCompiler.</p>
<p>Code folgt:</p>
<p>char* Log::cint(int x, Uint32 radix) {<br />
if (m_cint)<br />
delete[] m_cint;</p>
<p>int i = -1;<br />
Uint32 j = 0;</p>
<p>char t;<br />
char ts[10];</p>
<p>bool sign;</p>
<p>if (sign = (bool)(x &amp; 0x80000000)) { //check if value is signed<br />
x ^= 0x80000000; //delete sign bit<br />
}</p>
<p>if (x) { //check if x is zero<br />
while (x) {<br />
i++;<br />
t = x % radix; //example: 23 mod 10 = 3<br />
x /= radix; //example: 23 / 10 = 2</p>
<p>t |= 0x30; //numberchars begin with 0x30<br />
ts[i] = t; //copy new char to array<br />
}<br />
} else {<br />
ts[0] = '0'; //temp string is just 0<br />
}</p>
<p>if(sign)<br />
ts[++i] = '-'; //if there was a sign, add it</p>
<p>m_cint = new char[i+1]; //create a new string<br />
while (i&gt;=0) { //copy reverse to string... example: {2,3} -&gt; {3,2}<br />
m_cint[j] = ts[i];<br />
j++;<br />
i--;<br />
}</p>
<p>return m_cint;<br />
}</p>
<p>Ich weiß, sieht extrem grausam aus... hoffe ihr versteht was es tut --&gt; wandelt integer in einen char um <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/topic/154924/new-char-i-immer-10-chars-lang</link><generator>RSS for Node</generator><lastBuildDate>Tue, 08 Sep 2026 03:27:14 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/154924.rss" rel="self" type="application/rss+xml"/><pubDate>Tue, 01 Aug 2006 00:24:49 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to new char[i] immer 10 chars lang on Tue, 01 Aug 2006 00:24:49 GMT]]></title><description><![CDATA[<p>Wieder eine ganz einfache Frage... hoffentlich hat sie auch eine einfach Lösung --&gt; warum ist der neue char[] immer 10 chars groß?<br />
Kompiliere mit dem MSCompiler.</p>
<p>Code folgt:</p>
<p>char* Log::cint(int x, Uint32 radix) {<br />
if (m_cint)<br />
delete[] m_cint;</p>
<p>int i = -1;<br />
Uint32 j = 0;</p>
<p>char t;<br />
char ts[10];</p>
<p>bool sign;</p>
<p>if (sign = (bool)(x &amp; 0x80000000)) { //check if value is signed<br />
x ^= 0x80000000; //delete sign bit<br />
}</p>
<p>if (x) { //check if x is zero<br />
while (x) {<br />
i++;<br />
t = x % radix; //example: 23 mod 10 = 3<br />
x /= radix; //example: 23 / 10 = 2</p>
<p>t |= 0x30; //numberchars begin with 0x30<br />
ts[i] = t; //copy new char to array<br />
}<br />
} else {<br />
ts[0] = '0'; //temp string is just 0<br />
}</p>
<p>if(sign)<br />
ts[++i] = '-'; //if there was a sign, add it</p>
<p>m_cint = new char[i+1]; //create a new string<br />
while (i&gt;=0) { //copy reverse to string... example: {2,3} -&gt; {3,2}<br />
m_cint[j] = ts[i];<br />
j++;<br />
i--;<br />
}</p>
<p>return m_cint;<br />
}</p>
<p>Ich weiß, sieht extrem grausam aus... hoffe ihr versteht was es tut --&gt; wandelt integer in einen char um <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="🙂"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1108119</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1108119</guid><dc:creator><![CDATA[CrazyCrow]]></dc:creator><pubDate>Tue, 01 Aug 2006 00:24:49 GMT</pubDate></item><item><title><![CDATA[Reply to new char[i] immer 10 chars lang on Tue, 01 Aug 2006 00:28:39 GMT]]></title><description><![CDATA[<p>Entschuldigt... bin müde und außerdem langsam wirklich ... nicht gut gelaunt.</p>
<pre><code>char* Log::cint(int x, Uint32 radix) {
	if (m_cint) 
		delete[] m_cint;

	int i = -1;
	Uint32 j = 0;

	char t;
	char ts[10];

	bool sign;

	if (sign = (bool)(x &amp; 0x80000000)) { //check if value is signed
		x ^= 0x80000000; //delete sign bit
	} 

	if (x) { //check if x is zero
		while (x) {
			i++;
			t = x % radix; //example: 23 mod 10 = 3
			x /= radix; //example: 23 / 10 = 2

			t |= 0x30; //numberchars begin with 0x30
			ts[i] = t; //copy new char to array
		}	
	} else {
		ts[0] = '0'; //temp string is just 0
	}

	if(sign)
		ts[++i] = '-'; //if there was a sign, add it

	m_cint = new char[i+1]; //create a new string
	while (i&gt;=0) { //copy reverse to string... example: {2,3} -&gt; {3,2}
		m_cint[j] = ts[i];
		j++;
		i--;
	}

	return m_cint;		
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1108120</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1108120</guid><dc:creator><![CDATA[CrazyCrow]]></dc:creator><pubDate>Tue, 01 Aug 2006 00:28:39 GMT</pubDate></item><item><title><![CDATA[Reply to new char[i] immer 10 chars lang on Tue, 01 Aug 2006 01:06:45 GMT]]></title><description><![CDATA[<p>Super, ein Triplepost <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title="=("
      alt="😞"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f621.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--pouting_face"
      title=":rage:"
      alt="😡"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f44e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--thumbs_down"
      title=":-1:"
      alt="👎"
    /> <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f61e.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--disappointed_face"
      title=":("
      alt="😞"
    /></p>
<p>Ich scheine in letzter Zeit wirklich garnichts zu packen und nur Mist zu machen.</p>
<p>Hier noch eine Lösung... (ist eben auf Geschwindigkeit ausgelegt):</p>
<p>char* Log::cint(int x, Uint32 radix) {<br />
int i = -1;<br />
Uint32 j = 0;</p>
<p>char t;<br />
bool sign;</p>
<p>if (sign = (bool)(x &amp; 0x80000000)) { //check if value is signed<br />
x ^= 0x80000000;//delete sign bit<br />
x = 0x80000000 - x;<br />
}</p>
<p>if (x) { //check if x is zero<br />
while (x) {<br />
i++;<br />
t = x % radix; //example: 23 mod 10 = 3<br />
x /= radix; //example: 23 / 10 = 2</p>
<p>t |= 0x30; //numberchars begin with 0x30<br />
cint_s.m_temp[i] = t; //copy new char to array<br />
}<br />
} else {<br />
cint_s.m_temp[0] = '0'; //temp string is just 0<br />
}</p>
<p>if(sign)<br />
cint_s.m_temp[++i] = '-'; //if there was a sign, add it</p>
<p>while (i&gt;=0) { //copy reverse to string... example: {2,3} -&gt; {3,2}<br />
cint_s.m_cint[j] = cint_s.m_temp[i];<br />
j++;<br />
i--;<br />
}</p>
<p>cint_s.m_cint[j] = NULL;</p>
<p>return cint_s.m_cint;<br />
}</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1108127</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1108127</guid><dc:creator><![CDATA[CrazyCrow]]></dc:creator><pubDate>Tue, 01 Aug 2006 01:06:45 GMT</pubDate></item><item><title><![CDATA[Reply to new char[i] immer 10 chars lang on Tue, 01 Aug 2006 04:46:14 GMT]]></title><description><![CDATA[<p>CrazyCrow schrieb:</p>
<blockquote>
<pre><code>if (sign = (bool)(x &amp; 0x80000000)) { //check if value is signed
		x ^= 0x80000000; //delete sign bit
	}
</code></pre>
</blockquote>
<p>Das ist ja fast schon ein Fall für thedailywtf.</p>
<pre><code class="language-cpp">if( sign = (x &lt; 0) )
{
    x = -x;
}
</code></pre>
<p>Und zu deiner Frage: Nullterminierung?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1108140</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1108140</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Tue, 01 Aug 2006 04:46:14 GMT</pubDate></item><item><title><![CDATA[Reply to new char[i] immer 10 chars lang on Tue, 01 Aug 2006 06:06:39 GMT]]></title><description><![CDATA[<pre><code class="language-cpp">char ts[10];
</code></pre>
<p>Viel mehr als 10 Einträge hat dein ts auch garnicht.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1108156</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1108156</guid><dc:creator><![CDATA[David_pb]]></dc:creator><pubDate>Tue, 01 Aug 2006 06:06:39 GMT</pubDate></item><item><title><![CDATA[Reply to new char[i] immer 10 chars lang on Tue, 01 Aug 2006 10:32:07 GMT]]></title><description><![CDATA[<p>Zu MFK:</p>
<p>Ja, habe ich mir schon gedacht ^^. Aber ich dachte mir ein bitwise-And und ein Bitwise-xor sind immernoch schneller ... dabei ist das völliger Quatsch.</p>
<p>Zu David_pb:</p>
<p>Ne, kann nicht länger werden, aber ... ich weiß es auch nicht.. ich war zu müde hoffe ich.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1108375</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1108375</guid><dc:creator><![CDATA[CrazyCrow]]></dc:creator><pubDate>Tue, 01 Aug 2006 10:32:07 GMT</pubDate></item><item><title><![CDATA[Reply to new char[i] immer 10 chars lang on Tue, 01 Aug 2006 11:17:23 GMT]]></title><description><![CDATA[<p>CrazyCrow schrieb:</p>
<blockquote>
<p>Ja, habe ich mir schon gedacht ^^. Aber ich dachte mir ein bitwise-And und ein Bitwise-xor sind immernoch schneller ... dabei ist das völliger Quatsch.</p>
</blockquote>
<p>Ich vermute, das langsamste an deinem Code dürfte der dynamische Speicher sein. Aber das kann nur ein Profiler wirklich klären.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1108410</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1108410</guid><dc:creator><![CDATA[MFK]]></dc:creator><pubDate>Tue, 01 Aug 2006 11:17:23 GMT</pubDate></item></channel></rss>