Convert String from EBCDIC to Unicode / UTF8

为君一笑 提交于 2019-12-31 03:08:11

问题


I'm able to send messages from Java to Websphere MQ on AS400. If I send messages from WinXP, there is no difference if I use any accessible Locale, including full Language Localization; nor is there a problem with English Locale. Important for correct encoding is only this code line:

msgId.characterSet = 1208;

Infortunately, it's not valid. There are differences in the case that I want to take the message from MQ Queue.

  • In the case that I get message from MQ Queue from Windows OS (English without any of Language Pack), I'm able to take message from MQ Queue in String form.

  • In the case that Windows OS is Full Localized with some of Language Pack, I am receiving an exception

Exception occured while to message buffer :
    java.io.UnsupportedEncodingException: Cp870 

from code (removed balast from try - catch - finally block)

try {
    Class.forName("com.ibm.mq.MQEnvironment");
    MQEnvironment.hostname = hostname;
    MQEnvironment.port = port1;
    MQEnvironment.channel = channel;
    MQEnvironment.userID = userID;
    MQEnvironment.properties.put(MQC.TRANSPORT_PROPERTY, MQC.TRANSPORT_MQSERIES);
    try {
        qmgr1 = new MQQueueManager(qmanager);
        MQGetMessageOptions gmo = new MQGetMessageOptions();
        int openOptions2 = MQC.MQOO_INPUT_SHARED;
        gmo.options = MQC.MQGMO_NO_SYNCPOINT; //Set no sync point
        gmo.options = MQC.MQGMO_CONVERT; //Handles ASCII/EBCDIC
        gmo.options = MQC.MQGMO_WAIT; //Wait until message arrives
        gmo.waitInterval = 3000;
        getq = qmgr1.accessQueue(responseQueue, openOptions2);
        while (true) {
            try {
                responseFromQUEUE = "";
                MQMessage msgId = new MQMessage();
                //putmsg.characterSet = 437;// Set code page to ASCII
                //putmsg.characterSet = 1200;// Set code page to UNICODE
                msgId.characterSet = 1208;
                getq.get(msgId, gmo);
                String str = msgId.readLine();
                //responseFromQUEUE = CpConv.tf870to1250(msgId.readLine());
                //own EncodePage doesn't works too
                if (responseFromQUEUE.length() == 0) {
                    break;
                }
            } catch (MQException mqx) {
                if (mqx.reasonCode == EMPTY_QUEUE) {
                } else {
                }
            }
        }
    } catch (MQException mqx) {
    } finally {
    }
} catch (IOException ex) {
} catch (ClassNotFoundException e) {
}

please

  • is there some parameter(s), something that I miss there for take String from MQMessage#readLine()

  • my question here is only about String conversion,

  • I haven't any issue with Encode from MQMessage#getBytes


回答1:


CP870 is EBCDIC host code page. To what locale are you changing the Windows to when the exception occurs? Do you still use msgId.characterSet = 1208; after the local is changed? It looks like Java libraries are unable to convert the incoming message that is in CP870 to your current locale.

Check what is the Windows code page when you change the locale and see if 1208 is correct for msgId.characterSet.




回答2:


 gmo.options = MQC.MQGMO_NO_SYNCPOINT; //Set no sync point
 gmo.options = MQC.MQGMO_CONVERT; //Handles ASCII/EBCDIC
 gmo.options = MQC.MQGMO_WAIT; //Wait until message arrives

this code is wrong, you are overwriting the option, you need | them.



来源:https://stackoverflow.com/questions/11056776/convert-string-from-ebcdic-to-unicode-utf8

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!