<?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[char array to int]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe ein Problem und zwar ich möchte ein char array in int umwandlen ich benutze die zwei folgende funktionnen:</p>
<pre><code class="language-cpp">char result[30]={0x84,0x37,0xfe,0x04,0xfc,0xfe,0x84,0x37,0xfc};
 string 	inetrm;
		 stringstream ss;
		 ss &lt;&lt; result;
		 ss&gt;&gt;inetrm;
Hour =static_cast&lt;int&gt;(strtol(inetrm.substr( 6, 2).c_str(),NULL,16));
				 Day  =static_cast&lt;int&gt;(strtol(inetrm.substr( 4, 2).c_str(),NULL,16));
				 Month=static_cast&lt;int&gt;(strtol(inetrm.substr( 2, 2).c_str(),NULL,16));
</code></pre>
<p>aber ich bekomme immer null</p>
<p>das hier habe ich auch versuch :</p>
<pre><code class="language-cpp">char man[2];
man[1]={0x00};
man[0] = result[3];
int Year = atoi(man);
</code></pre>
<p>die erste Variante hat glaube ich schon in einem anderen Programm funktionniert,<br />
warum ich jetzt immer 0 bekomme weißt ich nicht. Kann mir vielleicht jemand sagen was ich da falsch mache oder wie ich das anders machen kann ?</p>
<p>danke</p>
<p>Ryoban</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/306943/char-array-to-int</link><generator>RSS for Node</generator><lastBuildDate>Fri, 07 Aug 2026 03:02:30 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/306943.rss" rel="self" type="application/rss+xml"/><pubDate>Sat, 11 Aug 2012 14:48:23 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to char array to int on Sat, 11 Aug 2012 14:48:23 GMT]]></title><description><![CDATA[<p>Hallo zusammen,</p>
<p>ich habe ein Problem und zwar ich möchte ein char array in int umwandlen ich benutze die zwei folgende funktionnen:</p>
<pre><code class="language-cpp">char result[30]={0x84,0x37,0xfe,0x04,0xfc,0xfe,0x84,0x37,0xfc};
 string 	inetrm;
		 stringstream ss;
		 ss &lt;&lt; result;
		 ss&gt;&gt;inetrm;
Hour =static_cast&lt;int&gt;(strtol(inetrm.substr( 6, 2).c_str(),NULL,16));
				 Day  =static_cast&lt;int&gt;(strtol(inetrm.substr( 4, 2).c_str(),NULL,16));
				 Month=static_cast&lt;int&gt;(strtol(inetrm.substr( 2, 2).c_str(),NULL,16));
</code></pre>
<p>aber ich bekomme immer null</p>
<p>das hier habe ich auch versuch :</p>
<pre><code class="language-cpp">char man[2];
man[1]={0x00};
man[0] = result[3];
int Year = atoi(man);
</code></pre>
<p>die erste Variante hat glaube ich schon in einem anderen Programm funktionniert,<br />
warum ich jetzt immer 0 bekomme weißt ich nicht. Kann mir vielleicht jemand sagen was ich da falsch mache oder wie ich das anders machen kann ?</p>
<p>danke</p>
<p>Ryoban</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2241008</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2241008</guid><dc:creator><![CDATA[ryoban]]></dc:creator><pubDate>Sat, 11 Aug 2012 14:48:23 GMT</pubDate></item><item><title><![CDATA[Reply to char array to int on Sat, 11 Aug 2012 16:00:04 GMT]]></title><description><![CDATA[<p>Du gibst wenig Information zu deinem Problem an - welche Arrays, welche Deklarationen, wieso <code>C-Style Arrays</code> ?</p>
<p><strong>Angenommen du willst einfach ein <code>char</code> -Array in ein <code>int</code> -Array kopieren:</strong></p>
<p>Hier muss man sich im klaren sein, dass das zwei verschieden große Skalare sind. Einfach<a href="http://www.cplusplus.com/reference/clibrary/cstring/memcpy/" rel="nofollow">** <code>std::memcpy()</code> **</a>verwenden wird also nicht das gewünschte Ergebnis bringen.</p>
<p>Es gibt zum einen die Möglichkeit einfach von Anfang an einen <a href="http://www.cplusplus.com/reference/stl/" rel="nofollow"><strong>STL-Container</strong></a> zu verwenden. Dann gibt es ganz einfach in jedem Container einen start-end Iteratorenpaar Konstruktor:</p>
<pre><code class="language-cpp">std::vector&lt;char&gt; myChars;
myChars.push_back('A');
myChars.push_back('\a');

std::vector&lt;int&gt; myIntArr(a.begin(), a.end());
</code></pre>
<p>Das ist definitiv die beste Lösung. Sieh dir<a href="http://www.cplusplus.com/reference/stl/vector/" rel="nofollow">** <code>std::vector</code> **</a>einfach einmal an.</p>
<p><strong>Zum Code:</strong></p>
<pre><code class="language-cpp">char result[30]={0x84,0x37,0xfe,0x04,0xfc,0xfe,0x84,0x37,0xfc}; 
 string     inetrm; 
         stringstream ss; 
         ss &lt;&lt; result;
</code></pre>
<p>Das geht schon in Zeile 4 nicht gut - du schreibst gerade mal die <strong>Adresse des ersten Arrayelementes</strong> in den Stringstream. Der Rest, da bin ich zu faul zum Analysieren.</p>
<p>Wie dein Code funktionieren könnte:</p>
<pre><code class="language-cpp">std::stringstream stream;

        char arr[]{'a', 'b', 'c', 'd'};

        std::copy(arr, arr + sizeof arr, std::ostream_iterator&lt;int&gt;(stream, &quot; &quot;));

        int ints[sizeof arr];

        std::copy(std::istream_iterator&lt;int&gt;(stream), std::istream_iterator&lt;int&gt;(), ints);
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2241022</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2241022</guid><dc:creator><![CDATA[Sone]]></dc:creator><pubDate>Sat, 11 Aug 2012 16:00:04 GMT</pubDate></item></channel></rss>