title: Convert byte array / char array to hexidecimal string in C
body: <p>I think my understanding of bytes arrays and char arrays is causing me some issues, here is my problem:</p>
  
  <p>I have an application that pulls messages from Websphere MQ and sends them onto a target system.</p>
  
  <p>A MQ message has a MQBYTE24 (byte array 24 essentially) that represents the MSGID of the message. My goal is to convert this to a hexidecimal string.</p>
  
  <p>On the WMQ explorer on my Linux box message 1 in the queue has a message identifier of "AMQ QM01" (at least that it what it looks like), and the bytes are below as displayed in the explorer:</p>
  
  <pre><code>00000   41 4D 51 20 51 4D 30 31--20 20 20 20 20 20 20 20  |AMQ QM01        |
  00010   BD F4 A8 4E A2 A3 06 20--                         |...N...         |
  </code></pre>
  
  <p>Now when my code runs I pick up that same message id and try convert it to a hex string.</p>
  
  <p>The exact message id while debugging is:</p>
  
  <p><strong>AMQ QM01        \275\364\250N\242\243\006</strong></p>
  
  <p>And after running through my conversion (code below) i get:</p>
  
  <p><strong>414D5120514D30312020202020202020FFFFFF4EFFFF6</strong></p>
  
  <p>As you can see it is slightly different to the one that the WMQ Explorer shows, any idea what i am doing wrong here?</p>
  
  <p>I assume it is me converting from the MQBYTE24 to char....something is going wrong there...</p>
  
  <p>Below is a small sample program that produces the "wrong result".....i assune i must use a byte array instead of char?</p>
  
  <p>The output for the following is:</p>
  
  <p>Result: 414D5120514D30312020202020202020FFFFFF4EFFFF6</p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;string.h&gt;
  #include &lt;stdlib.h&gt;
  
  int main(){   
      char name[41]="AMQ QM01        \275\364\250N\242\243\006";
      char buffer[82]="";
      char *pbuffer = buffer;
      FILE *fp_1;
      FILE *fp_2;
      int size;
      char *buffer_1 = NULL;
      char *buffer_2 = NULL;
  
      int rc = convertStrToHex(buffer, name);
      printf( "Result: %s\n", pbuffer ); 
      }
      return 0;
  }
  
  int convertStrToHex(char *buffer, char str[10]){
      int len = strlen(str);
      int i;
  
      for( i = 0; i &lt; len ;i++ ){
          sprintf(buffer, "%X", str[i]);
          buffer +=2;
      };
  }
  </code></pre>
  
  <p>Thanks for the help :-)</p>
  
  <p>Lynton</p>