title: Get the Correlation ID from MQ as string
body: <p>I am setting the Correlation ID while sending the message to MQ. And I need to use the CorrelationID from the COA of the message I pushed for the further processing. </p>
  
  <p>I am setting the correlation ID and sending the message to queue using the below code.</p>
  
  <pre><code>MQMessage message = createMQMessage("12345");
  message.write("Some message to push".getBytes());
  queue.put(message);
  
  private MQMessage createMQMessage(String corrID){
  
          MQMessage message = new MQMessage();
          message.messageFlags = MQConstants.MQMF_SEGMENTATION_ALLOWED;
  
          if (ackQueueName != null) {
  
              message.messageType = MQConstants.MQMT_REQUEST;
              message.replyToQueueManagerName = ackQueueManagerName;
              message.replyToQueueName = ackQueueName;
              message.report = MQConstants.MQRO_COA | MQConstants.MQRO_COD;
              message.correlationId = corrID.getBytes();
  
  
  
          }
          return message;
      }
  </code></pre>
  
  <p>I am reading the replyQueue from another application to get the COA and extract the correlation ID for further processing. </p>
  
  <p>But the correlation ID is in <code>byte[]</code> format and I used the below method <code>getHexString</code> to get the string. But all I got is 48 digit Hex format of my correlation ID like </p>
  
  <blockquote>
    <p>414d5120514d41444556202020202020b5ca0d5b13b3bb20</p>
  </blockquote>
  
  <pre><code>public static String getHexString(byte[] b) throws Exception {
          String result = "";
          for (int i=0; i &lt; b.length; i++) {
              result += Integer.toString( ( b[i] &amp; 0xff ) + 0x100, 16).substring( 1 );
          }
  
  
          return result;
  
      }
  </code></pre>
  
  <p>All I need is the approach to convert the <strong>48digit HexString</strong> to the Original Correlation ID I set. I tried using the below method to convert, but its giving me the junk data.</p>
  
  <pre><code>public static String hexStringToByteArray(String hex) {
          int l = hex.length();
          byte[] data = new byte[l/2];
          for (int i = 0; i &lt; l; i += 2) {
              data[i/2] = (byte) ((Character.digit(hex.charAt(i), 16) &lt;&lt; 4)
                      + Character.digit(hex.charAt(i+1), 16));
          }
          return new String(data);
      }
  </code></pre>