简体   繁体   中英

Convert byte array / char array to hexidecimal string in C

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

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.

尝试

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

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]);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM