<?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[ASCII-String -&amp;gt; Hex-String (binary)]]></title><description><![CDATA[<p>In einer XML Datei, die ich einlese, stehen Daten in ASCII formatiert, die ich binär in eine Datei schreiben muss.</p>
<p>Sollte also im String &quot;AABBCC&quot; stehen, dann muss nachher in der Datei auch AABBCC stehen, aber im Sinne von 0xAA 0xBB und 0xCC.</p>
<p>Ich habe das in die andere Richtung schon gemacht, da hatte ich es so gelöst:</p>
<pre><code class="language-cpp">char* HexToASCII(char* Payload, unsigned int Length)
{
  // if length is zero, return null pointer
  if (Length == 0) return &quot;&quot;;
  // temp array will contain the complete payload
  char* tempArray = new char[Length*2+1];
  // step through each char and convert it to its ascii aquivalent
  for (unsigned int i=0; i &lt; Length; i++)
  {
    // write 3 characters to the temp array and overwrite the terminating zero
    sprintf_s(tempArray+i*2, 3, &quot;%02X&quot;, Payload[i]);
  }

  return tempArray;
}
</code></pre>
<p>Aber andersrum komm ich nicht drauf.</p>
<p>Weiß da wer weiter?</p>
<p>Danke!</p>
<p>Gruß</p>
<p>Shinzo</p>
]]></description><link>https://www.c-plusplus.net/forum/topic/278665/ascii-string-gt-hex-string-binary</link><generator>RSS for Node</generator><lastBuildDate>Mon, 24 Aug 2026 21:16:46 GMT</lastBuildDate><atom:link href="https://www.c-plusplus.net/forum/topic/278665.rss" rel="self" type="application/rss+xml"/><pubDate>Fri, 10 Dec 2010 14:30:07 GMT</pubDate><ttl>60</ttl><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Fri, 10 Dec 2010 14:35:22 GMT]]></title><description><![CDATA[<p>In einer XML Datei, die ich einlese, stehen Daten in ASCII formatiert, die ich binär in eine Datei schreiben muss.</p>
<p>Sollte also im String &quot;AABBCC&quot; stehen, dann muss nachher in der Datei auch AABBCC stehen, aber im Sinne von 0xAA 0xBB und 0xCC.</p>
<p>Ich habe das in die andere Richtung schon gemacht, da hatte ich es so gelöst:</p>
<pre><code class="language-cpp">char* HexToASCII(char* Payload, unsigned int Length)
{
  // if length is zero, return null pointer
  if (Length == 0) return &quot;&quot;;
  // temp array will contain the complete payload
  char* tempArray = new char[Length*2+1];
  // step through each char and convert it to its ascii aquivalent
  for (unsigned int i=0; i &lt; Length; i++)
  {
    // write 3 characters to the temp array and overwrite the terminating zero
    sprintf_s(tempArray+i*2, 3, &quot;%02X&quot;, Payload[i]);
  }

  return tempArray;
}
</code></pre>
<p>Aber andersrum komm ich nicht drauf.</p>
<p>Weiß da wer weiter?</p>
<p>Danke!</p>
<p>Gruß</p>
<p>Shinzo</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1992624</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992624</guid><dc:creator><![CDATA[RyoShinzo]]></dc:creator><pubDate>Fri, 10 Dec 2010 14:35:22 GMT</pubDate></item><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Fri, 10 Dec 2010 15:06:06 GMT]]></title><description><![CDATA[<p>Das sieht mir eher nach C als nach C++ aus, aber in beiden Sprachen gilt: Die Standardeingaberoutinen können bereits Zahlen in Hexadezimalschreibweise verstehen. Benutze sie einfach und du hast dein Problem gelöst.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1992655</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992655</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 10 Dec 2010 15:06:06 GMT</pubDate></item><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Fri, 10 Dec 2010 15:21:37 GMT]]></title><description><![CDATA[<p>Wie meinst du das?</p>
<p>Ich habe einen Pointer, der auf einen char-Array zeigt, in dem die Hex-Zahlen als ASCII-Zeichen abgelegt sind. Also zum Beispiel:</p>
<p>arrayASCII[0] = 'A'; arrayASCII[1] = 'B'; arrayASCII[2] = 'C'; arrayASCII[3] = 'D';</p>
<p>Nun möchte ich aber, dass diese Daten wieder binär drin stehen, also so:</p>
<p>arrayBinaer[0] = 0xAB; arrayBinaer[1] = 0xCD;</p>
<p>Wer wandelt mir das dann so um?</p>
<p>Danke!</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1992665</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992665</guid><dc:creator><![CDATA[RyoShinzo]]></dc:creator><pubDate>Fri, 10 Dec 2010 15:21:37 GMT</pubDate></item><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Fri, 10 Dec 2010 15:34:52 GMT]]></title><description><![CDATA[<p>Welche Sprache? Dein Code sieht nach C aus, du fragst im C++ Forum. In C sscanf, in C++ ein stringstream.</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1992675</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992675</guid><dc:creator><![CDATA[SeppJ]]></dc:creator><pubDate>Fri, 10 Dec 2010 15:34:52 GMT</pubDate></item><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Fri, 10 Dec 2010 15:38:22 GMT]]></title><description><![CDATA[<p>Ich programmiere in C++.</p>
<p>Sollte sscanf aber perfomanter als stringstream sein, dann werde ich auch dazu greifen.</p>
<p>Ich danke dir, werde mir sscanf und stringstream mal genauer anschauen.</p>
<p>€: myClass:: vor der Methode habe ich weggelassen, da es ja nicht wirklich wichtig ist für das Verständnis meines Problems. Oder gibts in C++ noch eine einfachere Möglichkeit, meine Methode zu implementieren (also die, die ich gepostet habe)?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1992677</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992677</guid><dc:creator><![CDATA[RyoShinzo]]></dc:creator><pubDate>Fri, 10 Dec 2010 15:38:22 GMT</pubDate></item><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Fri, 10 Dec 2010 17:06:57 GMT]]></title><description><![CDATA[<p>RyoShinzo schrieb:</p>
<blockquote>
<p>Sollte sscanf aber perfomanter als stringstream sein, dann werde ich auch dazu greifen.</p>
</blockquote>
<p>Wenn es dir auf Performanz ankommt, mache diese einfachen Sachen besser selbst und ohne Lib-Funktionen, die für weitaus universellere Anwendungsfälle gedacht sind:</p>
<pre><code class="language-cpp">#define CHR2BIN(c) (c-(c&gt;='A'?55:48))
...
arrayBinaer[0] = CHR2BIN(arrayASCII[0])&lt;&lt;4 | CHR2BIN(arrayASCII[1]);
arrayBinaer[1] = CHR2BIN(arrayASCII[2])&lt;&lt;4 | CHR2BIN(arrayASCII[3]);
usw.
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1992717</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1992717</guid><dc:creator><![CDATA[Wutz]]></dc:creator><pubDate>Fri, 10 Dec 2010 17:06:57 GMT</pubDate></item><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Wed, 15 Dec 2010 08:50:34 GMT]]></title><description><![CDATA[<p>Wutz schrieb:</p>
<blockquote>
<p>Wenn es dir auf Performanz ankommt, mache diese einfachen Sachen besser selbst und ohne Lib-Funktionen, die für weitaus universellere Anwendungsfälle gedacht sind:</p>
<pre><code class="language-cpp">#define CHR2BIN(c) (c-(c&gt;='A'?55:48))
...
arrayBinaer[0] = CHR2BIN(arrayASCII[0])&lt;&lt;4 | CHR2BIN(arrayASCII[1]);
arrayBinaer[1] = CHR2BIN(arrayASCII[2])&lt;&lt;4 | CHR2BIN(arrayASCII[3]);
usw.
</code></pre>
</blockquote>
<p>Danke für den Code!</p>
<p>Wenn ich das allerdings richtig interpretiere, dann müsste man eigentlich, wenn c &gt;= 'A' true ergibt, 65 und nicht 55 abziehen, oder?</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1994756</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1994756</guid><dc:creator><![CDATA[RyoShinzo]]></dc:creator><pubDate>Wed, 15 Dec 2010 08:50:34 GMT</pubDate></item><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Wed, 15 Dec 2010 11:55:45 GMT]]></title><description><![CDATA[<p>Falls der Wert in Dener Datei in Wirklichkeit einen <code>int</code> Wert darstellen sollte, brauchst Du nicht über die einzelnen Character zu gehen:</p>
<pre><code class="language-cpp">#include &lt;sstream&gt;

int hexString2Int( const std::string&amp; hex_ )
{
    std::stringstream converter;
    converter &lt;&lt; std::hex &lt;&lt; hex_;
    int num;
    converter &gt;&gt; num;
    return num;
}
</code></pre>
]]></description><link>https://www.c-plusplus.net/forum/post/1994825</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1994825</guid><dc:creator><![CDATA[minastaros]]></dc:creator><pubDate>Wed, 15 Dec 2010 11:55:45 GMT</pubDate></item><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Wed, 15 Dec 2010 12:04:32 GMT]]></title><description><![CDATA[<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/21506">@minastaros</a>: Das sind keine int Werte, sondern das ist ein hex-binary String der in ASCII Darstellung gespeichert wurde. Das Programm von Wutz sollte daher gut funktionieren. Hatte sowas in der Art auch vor, aber das war nicht so schön kurz wie seins. <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
]]></description><link>https://www.c-plusplus.net/forum/post/1994829</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1994829</guid><dc:creator><![CDATA[RyoShinzo]]></dc:creator><pubDate>Wed, 15 Dec 2010 12:04:32 GMT</pubDate></item><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Wed, 15 Dec 2010 12:10:52 GMT]]></title><description><![CDATA[<p>RyoShinzo schrieb:</p>
<blockquote>
<p>Wenn ich das allerdings richtig interpretiere, dann müsste man eigentlich, wenn c &gt;= 'A' true ergibt, 65 und nicht 55 abziehen, oder?</p>
</blockquote>
<h1>'A' -&gt; 65 dezimal<br />
0xA -&gt; 10 dezimal</h1>
<p>Differenz ist 55 und nicht 65 <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/1994832</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1994832</guid><dc:creator><![CDATA[Wutz]]></dc:creator><pubDate>Wed, 15 Dec 2010 12:10:52 GMT</pubDate></item><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Wed, 15 Dec 2010 12:34:56 GMT]]></title><description><![CDATA[<p>Auch dafür gibt es eine Lösung.<br />
Betrachte den zurückgegebenen String einfach als Container, der nachher den binären Inhalt hat. Man kann natürlich die Sache umbauen, dass das Byte direkt in das Array gesetzt wird, dann übergibt man den Pointer darauf halt als Argument der Funktion. Das Prinzip ist aber das gleiche.</p>
<pre><code class="language-cpp">std::string hexStringToBinary( const std::string&amp; source_ )
{
    size_t len = source_.length( );
    std::string binaryString;
    size_t pos = 0;

    // Bei ungerader Anzahl zuerst nur _ein_ Nibble nehmen
    if( 0 != ( len % 2 ) )
    {
        unsigned int byte = hexString2Int( source_.substr( pos++, 1 ) );
        binaryString.push_back( (char)byte );
    }

    for( ; pos &lt; len; pos += 2 )
    {
        unsigned int byte = hexString2Int( source_.substr( pos, 2 ) );
        binaryString.push_back( (char)byte );
    }

    return binaryString;
}
</code></pre>
<p>Edit: wir nehmen das ungerade erste Nibble einfach mit statt Fehlermeldung</p>
]]></description><link>https://www.c-plusplus.net/forum/post/1994835</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1994835</guid><dc:creator><![CDATA[minastaros]]></dc:creator><pubDate>Wed, 15 Dec 2010 12:34:56 GMT</pubDate></item><item><title><![CDATA[Reply to ASCII-String -&amp;gt; Hex-String (binary) on Wed, 15 Dec 2010 12:34:21 GMT]]></title><description><![CDATA[<p>Wutz schrieb:</p>
<blockquote>
<h1>'A' -&gt; 65 dezimal<br />
0xA -&gt; 10 dezimal</h1>
<p>Differenz ist 55 und nicht 65 <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>
</blockquote>
<p>Ich nehm alles zurück! <img
      src="https://www.c-plusplus.net/forum/plugins/nodebb-plugin-emoji/emoji/emoji-one/1f609.png?v=ab1pehoraso"
      class="not-responsive emoji emoji-emoji-one emoji--winking_face"
      title=";)"
      alt="😉"
    /></p>
<p><a class="plugin-mentions-user plugin-mentions-a" href="https://www.c-plusplus.net/forum/uid/21506">@minastaros</a>: Bevor ich meinen char-Array in einen String, dann in einen int, wieder in einen String und dann letztendlich wieder in einen char-Array umwandle, nehme ich lieber die Methode von Wutz, aber trotzdem danke! <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/1994850</link><guid isPermaLink="true">https://www.c-plusplus.net/forum/post/1994850</guid><dc:creator><![CDATA[RyoShinzo]]></dc:creator><pubDate>Wed, 15 Dec 2010 12:34:21 GMT</pubDate></item></channel></rss>