Search code examples
javajakarta-eebyteibm-mqmq

String to Bytes Conversion-If already byte and taking it as string


I have a tibco activity which is giving this output in Byte.

Now i have to use this in my java code and send it to Mainframe Queue.

I am taking it as a String bwmsg="wcLDxMXGx8=" and in my main method...

byte[] bArray = bwmsg.getBytes();
BytesMessage bytmsg = session.createBytesMessage(); 
bytmsg.writeBytes(bArray);
mqSender.sendToMQ(bytmsg,connection,sender,session);

OUTPUT: 77634c44784d584778383d

Am i doing wrong? Something which is already a byte I am reading it as a string and then again getting the bytes out of it and doing the workaround. Is this wrong?

If yes, can you please tell how can I take that bwmsg field as bytes and pass it on to the method.

I have Googled many links but not good help from them.


Solution

  • As mentioned by Jon Skeet in the above comment.I am just putting his words into code.

    USE javax.xml.bind.DatatypeConverter

    static String bwmsg="wcLDxMXGx8=" static byte[] array = DatatypeConverter.parseBase64Binary(bwmsg);

    And in the main method ,make the above fields static so that you can call them directly in main method. public static void main(String [] args){ //other code here . . BytesMessage bytmsg = session.createBytesMessage(); bytmsg.writeBytes(array); mqSender.sendToMQ(bytmsg,connection,sender,session);

    This solved my purpose hope it will help others too.