Miłosz Orzeł

.net, js, html, arduino, java... no rants or clickbaits.

Sending message to WebSphere MQ in the UTF-8

If you want to put a message encoded in UTF-8 to WebSphere MQ queue, be sure to set CharacterSet property of MQMessage object to 1208. If you do not, the text will be encoded using UTF-16 (CCSID 1200).

MQQueueManager queueManager = new MQQueueManager(...);
MQQueue queue = queueManager.AccessQueue(...);
MQPutMessageOptions putMessageOptions = new MQPutMessageOptions(...);
MQMessage message = new MQMessage();

message.Format = MQC.MQFMT_STRING;
message.CharacterSet = 1208;
message.WriteString("abcąćę");
queue.Put(message, putMessageOptions);

Strings in .NET are encoded with UTF-16. Sometimes however, it is worthwhile to use UTF-8 for information exchange. Why? Version 8 is less verbose because characters from the US-ASCII table are encoded using 1 byte instead of using 2 as in the case of UTF-16. So if you send the text consisting solely of that character set, you will use two times less space! In the case of Polish letters, characters will be encoded with 2 bytes (as for UTF-16).

Here is a comparison of bytes for the text "abcąćę":

UTF-8    61 62 63 C4 85 C4 87 C4 99
UTF-16   61 00 62 00 63 00 05 01 07 01 19 01