title: Swapping bytes in a QByteArray
body: <p>I want to write a simple function to swap bytes in a <code>QByteArray</code>. This is what I have come up with:</p>
  
  <pre><code>int main(int argc, char *argv[])
  {
      QCoreApplication a(argc, argv);
  
      QString t = "abcde";
      QByteArray test;
      test.append(t.at(0));
      test.append(t.at(1));
      test.append(t.at(2));
      test.append(t.at(3));
      test.append(t.at(4));
  
      qDebug() &lt;&lt;  t;
      qDebug() &lt;&lt; QString(swapBytes(test,0,3));
  
      return a.exec();
  }
  
  QByteArray swapBytes(QByteArray in, int swapOffset, int quantity) {
      if (swapOffset &lt; 0) {
          return in;
      }
      if(quantity&gt;(in.length()/2)) {quantity=in.length()/2;}
      if(quantity &lt; 1) {quantity=1;}
      int k;
      char buf[quantity];
      char buf2[quantity];
      qDebug() &lt;&lt; quantity;
      for (int i = 0; i + quantity*2 + swapOffset &lt;= in.length(); i=i+2*quantity) {
          k=i;
          for(int b = 0;b&lt;i+quantity;b++){
              buf[b]=in.at(k);
              buf2[b]=in.at(k+swapOffset+quantity);
              k++;
          }
          qDebug() &lt;&lt; buf;
          qDebug() &lt;&lt; buf2;
          qDebug() &lt;&lt; in;
          in.replace(0,quantity,buf2);
          qDebug() &lt;&lt; in;
          in.replace(quantity+swapOffset,quantity,buf);
      }
      return in;
  }
  </code></pre>
  
  <p>For some reason when I run this code I get the following output :</p>
  
  <pre><code>abcde
  ab(
  cd
  abcde
  cdcde
  cdab(e
  </code></pre>
  
  <p>Where does the parentheses come from? As far as I know there is only one char per byte, so what is wrong?</p>