I think my understanding of bytes arrays and char arrays is causing me some issues, here is my problem:
I have an application that pulls messages from Websphere MQ and sends them onto a target system.
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.
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:
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... |
Now when my code runs I pick up that same message id and try convert it to a hex string.
The exact message id while debugging is:
AMQ QM01 \275\364\250N\242\243\006
And after running through my conversion (code below) i get:
414D5120514D30312020202020202020FFFFFF4EFFFF6
As you can see it is slightly different to the one that the WMQ Explorer shows, any idea what i am doing wrong here?
I assume it is me converting from the MQBYTE24 to char....something is going wrong there...
Below is a small sample program that produces the "wrong result".....i assune i must use a byte array instead of char?
The output for the following is:
Result: 414D5120514D30312020202020202020FFFFFF4EFFFF6
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
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 < len ;i++ ){
sprintf(buffer, "%X", str[i]);
buffer +=2;
};
}
Thanks for the help :-)
Lynton
convertStrToHex
before calling it. Either move the definition (which acts as a prototype in its own right) to beforemain
or add the plain prototype after the includes:int convertStrToHex(char *, char *);