Convert byte array / char array to hexadecimal string in C

I think my understanding of byte arrays and char arrays is causing some problems, here is my problem:

I have an application that retrieves messages from Websphere MQ and sends them to the target system.

The MQ message has MQBYTE24 (byte 24 essentially), which represents the MSGID of the message. My goal is to convert this to a hexadecimal string.

In the WMQ explorer on my Linux box, message 1 in the queue has the message identifier "AMQ QM01" (at least that's what it looks like), and the bytes below, as shown 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 that my code is running, I pick up the same message id and try to convert it to a hexadecimal string.

ID of the exact message during debugging:

AMQ QM01 \ 275 \ 364 \ 250N \ 242 \ 243 \ 006

And after completing my conversion (code below) I get:

414D5120514D30312020202020202020FFFFFFFF4EFFFF6

As you can see, this is a little different from what WMQ Explorer shows, any idea what I'm doing wrong here?

I assume that I am switching from MQBYTE24 to char .... there is something wrong ...

Below is a small sample program that creates the β€œwrong result” ..... I am assune, should I use a byte array instead of char?

The output for the following:

Result: 414D5120514D30312020202020202020FFFFFFFF4EFFFF6

 #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: -)

Linton

+4
source share
3 answers

As mentioned in several other answers, you need to specify unsigned char characters so that they are not padded with FF in order to fill in the 32-bit sum of int bytes. But in fact there is another problem: this single number 6 at the end will only print as one character in the output. You want each character to occupy exactly two positions, so you need a field specifier with zero fill. Putting it all together, you get

 sprintf(buffer, "%02X", (unsigned char)str[i]); 
+1
source

Depending on the compiler and platform, char is signed or not, and printf behavior is different.

Just draw str [i] on an unsigned char (or change the str type in the function 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 was considered unsafe to pass a string without allocated length and use sprintf. You should use snprintf with a real buffer length, or at least control the size limit inside the loop. If strlen (str) is larger than the buffer size * 2.

+2
source

Try

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

All Articles