5

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

4
  • are those escape sequences in there? How is the C sprintf handling them? Also, why the char *pbuffer = buffer? just use 'buffer' in the printf...
    – Aaron Gage
    Commented Oct 28, 2011 at 11:48
  • @AaronGage: they're octal character escapes. Commented Oct 28, 2011 at 11:59
  • You really, really should specify a prototype for convertStrToHex before calling it. Either move the definition (which acts as a prototype in its own right) to before main or add the plain prototype after the includes: int convertStrToHex(char *, char *);
    – pmg
    Commented Oct 28, 2011 at 12:04
  • It is just a test program....I do normally have prototypes ;-) Commented Oct 28, 2011 at 12:58

3 Answers 3

2

Depending on the compiler and platform char is signed or not and printf's behaviour is different.

Just cast str[i] to unsigned char (or change the type of str in the function's prototype) and it will work. For example (prototype changed):

int convertStrToHex(char *buffer, unsigned char str[10]){
    int len = strlen(str);
    int i;

    for( i = 0; i < len ;i++ ){
        sprintf(buffer, "%X", str[i]);
        buffer +=2;
    };
}

BTW: it considered as unsafe to pass a string without it's allocated length and to use sprintf. You should use snprintf with the real length of buffer or at least handle the size limit yourself inside the loop. In case strlen(str) is larger than buffer's size * 2.

1

As several other answers already point out, you need to cast the characters to unsigned char to avoid their being padded with FF to fill a 32-bit int's worth of bytes. But there's actually another issue: that lone number 6 at the end will only print as one character in the output. You want each character to take up exactly two positions, so you need a zero-padded field specifier. Putting it all together, you get

sprintf(buffer, "%02X", (unsigned char)str[i]);
1
  • Thanks to all of you, you have really helped alot! Commented Oct 28, 2011 at 13:06
1

Try

sprintf(buffer, "%X", (unsigned char)str[i]);

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.