<?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[Inhalt einer Datei hexadezimal ausgeben]]></title><description><![CDATA[<p><strong>Aufgabe:</strong><br />
Schreiben Sie ein Progamm, das den Inhalt einer Datei hexadezimal ausdruckt.<br />
Tipp: Da manche Compiler char als signed char implementieren, man aber für die Dar-<br />
stellung eine unsigned-Zahl braucht, muss ein Zeichen etwa wie folgt umgewandelt wer-<br />
den:</p>
<pre><code class="language-cpp">char c; 			// evtl. signed
while (datei.get(c)) { 		// lesen
	unsigned char uc = static_cast&lt;unsigned char&gt;(c);
	unsigned int zahl = static_cast&lt;unsigned int&gt;(uc);
	// zahl hier weiterverarbeiten ...
}
</code></pre>
<p><strong>Musterlösung:</strong></p>
<pre><code class="language-cpp">//  Datei hexadezimal ausgeben
// Hilfsmittel: Zeichen mit String ermitteln
#include &lt;cstdlib&gt;  // für exit()
#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main() {
  cout &lt;&lt; &quot;Dateiname :&quot;;
  string Quelldateiname;
  cin &gt;&gt; Quelldateiname;

  ifstream quelle(Quelldateiname, ios::binary);
  if (!quelle) {  // muss existieren
    cerr &lt;&lt; Quelldateiname &lt;&lt; &quot; kann nicht geöffnet werden!\n&quot;;
    exit(-1);
  }

  // char ist notwendig, weil get(unsigned char&amp;) nicht
  // implementiert ist (GNU C++)
  char c;  // signed!
  unsigned int count = 0;
  while (quelle.get(c)) {
    unsigned char uc = static_cast&lt;unsigned char&gt;(c);
    unsigned int zahl = static_cast&lt;unsigned int&gt;(uc);
    const string codierung{&quot;0123456789ABCDEF&quot;};
    // 2 stellig ausgeben
    cout &lt;&lt; codierung.at(zahl / 16) &lt;&lt; codierung.at(zahl % 16) &lt;&lt; ' ';
    if (++count % 20 == 0) {
      cout &lt;&lt; '\n';
    }
  }
  cout &lt;&lt; '\n';
}
</code></pre>
<p>Kann mir vielleicht jemand helfen das nachzuvollziehen?<br />
Ich führe das Programm aus und egal was ich für einen String eingebe, es kommt immer die Fehlermeldung cerr &lt;&lt; ...<br />
Die Aufgabenstellung verstehe ich so, dass der Benutzer einen String eingeben soll (also Datei = String).<br />
Soweit müsste ich erstmal kommen um zu versuchen den Rest des Programms nachzuvollziehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/350897/inhalt-einer-datei-hexadezimal-ausgeben</link><generator>RSS for Node</generator><lastBuildDate>Tue, 10 Nov 2026 00:29:51 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/350897.rss" rel="self" type="application/rss+xml"/><pubDate>Thu, 05 Mar 2020 14:32:45 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to Inhalt einer Datei hexadezimal ausgeben on Thu, 05 Mar 2020 14:32:45 GMT]]></title><description><![CDATA[<p><strong>Aufgabe:</strong><br />
Schreiben Sie ein Progamm, das den Inhalt einer Datei hexadezimal ausdruckt.<br />
Tipp: Da manche Compiler char als signed char implementieren, man aber für die Dar-<br />
stellung eine unsigned-Zahl braucht, muss ein Zeichen etwa wie folgt umgewandelt wer-<br />
den:</p>
<pre><code class="language-cpp">char c; 			// evtl. signed
while (datei.get(c)) { 		// lesen
	unsigned char uc = static_cast&lt;unsigned char&gt;(c);
	unsigned int zahl = static_cast&lt;unsigned int&gt;(uc);
	// zahl hier weiterverarbeiten ...
}
</code></pre>
<p><strong>Musterlösung:</strong></p>
<pre><code class="language-cpp">//  Datei hexadezimal ausgeben
// Hilfsmittel: Zeichen mit String ermitteln
#include &lt;cstdlib&gt;  // für exit()
#include &lt;fstream&gt;
#include &lt;iostream&gt;
#include &lt;string&gt;
using namespace std;

int main() {
  cout &lt;&lt; &quot;Dateiname :&quot;;
  string Quelldateiname;
  cin &gt;&gt; Quelldateiname;

  ifstream quelle(Quelldateiname, ios::binary);
  if (!quelle) {  // muss existieren
    cerr &lt;&lt; Quelldateiname &lt;&lt; &quot; kann nicht geöffnet werden!\n&quot;;
    exit(-1);
  }

  // char ist notwendig, weil get(unsigned char&amp;) nicht
  // implementiert ist (GNU C++)
  char c;  // signed!
  unsigned int count = 0;
  while (quelle.get(c)) {
    unsigned char uc = static_cast&lt;unsigned char&gt;(c);
    unsigned int zahl = static_cast&lt;unsigned int&gt;(uc);
    const string codierung{&quot;0123456789ABCDEF&quot;};
    // 2 stellig ausgeben
    cout &lt;&lt; codierung.at(zahl / 16) &lt;&lt; codierung.at(zahl % 16) &lt;&lt; ' ';
    if (++count % 20 == 0) {
      cout &lt;&lt; '\n';
    }
  }
  cout &lt;&lt; '\n';
}
</code></pre>
<p>Kann mir vielleicht jemand helfen das nachzuvollziehen?<br />
Ich führe das Programm aus und egal was ich für einen String eingebe, es kommt immer die Fehlermeldung cerr &lt;&lt; ...<br />
Die Aufgabenstellung verstehe ich so, dass der Benutzer einen String eingeben soll (also Datei = String).<br />
Soweit müsste ich erstmal kommen um zu versuchen den Rest des Programms nachzuvollziehen.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2587635</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2587635</guid><dc:creator><![CDATA[theAnfänger77]]></dc:creator><pubDate>Thu, 05 Mar 2020 14:32:45 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt einer Datei hexadezimal ausgeben on Thu, 05 Mar 2020 14:58:13 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/35167">@theAnfänger77</a> sagte in <a href="/forum/post/2587635">Inhalt einer Datei hexadezimal ausgeben</a>:</p>
<blockquote>
<p>Ich führe das Programm aus und egal was ich für einen String eingebe,</p>
</blockquote>
<p>Du musst schon den Namen einer existierenden Datei eingeben und nicht irgendeinen String.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2587639</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2587639</guid><dc:creator><![CDATA[wob]]></dc:creator><pubDate>Thu, 05 Mar 2020 14:58:13 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt einer Datei hexadezimal ausgeben on Fri, 06 Mar 2020 16:49:00 GMT]]></title><description><![CDATA[<p>Ah danke!<br />
In Zeile 14 heißt das Zauberwort HSC Datei.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2587715</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2587715</guid><dc:creator><![CDATA[theAnfänger77]]></dc:creator><pubDate>Fri, 06 Mar 2020 16:49:00 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt einer Datei hexadezimal ausgeben on Fri, 06 Mar 2020 17:27:55 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/35167">@theAnfänger77</a> Du kannst jede Datei nehmen. Auch das Programm selber.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2587719</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2587719</guid><dc:creator><![CDATA[DirkB]]></dc:creator><pubDate>Fri, 06 Mar 2020 17:27:55 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt einer Datei hexadezimal ausgeben on Sat, 07 Mar 2020 10:57:07 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/35167">@theAnfänger77</a> sagte in <a href="/forum/post/2587635">Inhalt einer Datei hexadezimal ausgeben</a>:</p>
<blockquote>
<pre><code>cout &lt;&lt; codierung.at(zahl / 16) &lt;&lt; codierung.at(zahl % 16) &lt;&lt; ' ';
</code></pre>
</blockquote>
<p><a href="https://en.cppreference.com/w/cpp/io/manip/hex" rel="nofollow">std::hex</a> ist Dein Freund.</p>
<p>//edit: ach so, und<br />
<a href="https://en.cppreference.com/w/cpp/io/manip/setw" rel="nofollow">std::setw()</a> und<br />
<a href="https://en.cppreference.com/w/cpp/io/manip/setfill" rel="nofollow">std::setfill()</a></p>
]]></description><link>https://www.c-plusplus.net/forum/post/2587723</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2587723</guid><dc:creator><![CDATA[Swordfish]]></dc:creator><pubDate>Sat, 07 Mar 2020 10:57:07 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt einer Datei hexadezimal ausgeben on Sat, 07 Mar 2020 10:46:14 GMT]]></title><description><![CDATA[<p>Das verstehe ich auch gar nicht:</p>
<pre><code class="language-cpp">cout &lt;&lt; codierung.at(zahl / 16) &lt;&lt; codierung.at(zahl % 16) &lt;&lt; ' ';
</code></pre>
<p>Für zahl = 15 wird dann 0 ausgegeben und 15 ist doch F.</p>
<p>Sowas dann eher noch:</p>
<pre><code class="language-cpp">unsigned int uc = 15;
std::cout &lt;&lt; std::hex &lt;&lt; uc &lt;&lt; ' ';
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/2587724</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2587724</guid><dc:creator><![CDATA[theAnfänger77]]></dc:creator><pubDate>Sat, 07 Mar 2020 10:46:14 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt einer Datei hexadezimal ausgeben on Sat, 07 Mar 2020 10:51:48 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/35167">@theAnfänger77</a> sagte in <a href="/forum/post/2587724">Inhalt einer Datei hexadezimal ausgeben</a>:</p>
<blockquote>
<p>cout &lt;&lt; <a href="http://codierung.at" rel="nofollow">codierung.at</a>(zahl / 16) &lt;&lt; <a href="http://codierung.at" rel="nofollow">codierung.at</a>(zahl % 16) &lt;&lt; ' ';</p>
</blockquote>
<p>Nö, für 15 wird daraus:</p>
<pre><code class="language-cpp">cout &lt;&lt; codierung.at(0) &lt;&lt; codierung.at(15) &lt;&lt; ' ';
</code></pre>
<p>also: 0F</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2587725</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2587725</guid><dc:creator><![CDATA[Belli]]></dc:creator><pubDate>Sat, 07 Mar 2020 10:51:48 GMT</pubDate></item><item><title><![CDATA[Reply to Inhalt einer Datei hexadezimal ausgeben on Sat, 07 Mar 2020 11:06:59 GMT]]></title><description><![CDATA[<p>Ah stimmt!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/2587726</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/2587726</guid><dc:creator><![CDATA[theAnfänger77]]></dc:creator><pubDate>Sat, 07 Mar 2020 11:06:59 GMT</pubDate></item></channel></rss>