Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/230.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 将字节转换为字符串的方法_Java_Ibm Mq - Fatal编程技术网

Java 将字节转换为字符串的方法

Java 将字节转换为字符串的方法,java,ibm-mq,Java,Ibm Mq,我使用下面的代码使用Java从IBM MQ读取消息 public void asyncReadMessage() { System.out.println(“内部异步读取消息”); 试一试{ com.ibm.mq.MQQueue defaultLocalQueue; MQQueueManager qManager=null; 字符串strMessage=null; MQEnvironment.hostname=“reese.int.westgroup.com”; MQEnvironment.ch

我使用下面的代码使用Java从IBM MQ读取消息

public void asyncReadMessage()
{
System.out.println(“内部异步读取消息”);
试一试{
com.ibm.mq.MQQueue defaultLocalQueue;
MQQueueManager qManager=null;
字符串strMessage=null;
MQEnvironment.hostname=“reese.int.westgroup.com”;
MQEnvironment.channel=“CLIENTCONNECTION”;
MQEnvironment.port=1414;
字符串qMngrStr=“”;
qManager=新的MQQueueManager(qMngrStr);
int openOptions=MQC.MQOO\u如果停止则失败|
MQC.MQOO_输入|u共享| MQC.MQOO_浏览;
//|MQC.MQOO_查询;
String queueName=“COMSERV.SRCHEXT.EVENTS.PUBLISH.QA.Q01”;
System.out.println(“访问::”+queueName);
defaultLocalQueue=qManager.accessQueue(queueName,openOptions);
//设置传输属性。
System.out.println(“设置MQ道具”);
MQEnvironment.properties.put(MQC.TRANSPORT_属性,MQC.TRANSPORT_MQSERIES_客户端);
System.out.println(“新队列管理器”);
ArrayList msglist=新的ArrayList();
布尔getMore=true;
字符串messageText=null;
while(获取更多)
{  
MQMessage getMessages=新MQMessage();
MQGetMessageOptions gmo=新的MQGetMessageOptions();
gmo.options=MQC.MQGMO_WAIT | MQC.MQGMO_BROWSE_FIRST;
gmo.matchOptions=MQC.MQMO_NONE;
gmo.waitInterval=100000;
defaultLocalQueue.get(getMessages,gmo);
int length=getMessages.getMessageLength();
字节[]bMsg=新字节[长度];
getMessages.readFully(bMsg);
String retrievedmsg=新字符串(bMsg);
System.out.println(“retrievedmsg::”+retrievedmsg);
如果(!(retrievedmsg.equals(“”))
{
MessageHandler mh=新MessageHandler();
System.out.println(“messagehandler java文件:“+retrievedmsg”);
mh.processMessages(retrievedmsg);
}
msglist.add(retrievedmsg);
System.out.println(“msglist::”+msglist.toString());
}
如果(defaultLocalQueue.getCurrentDepth()==0)
{
getMore=false;
}
/*for(字符串消息:msglist)
{
System.out.println(“循环中的消息::”+消息);
MessageHandler mh=新MessageHandler();
System.out.println(“messagehandler java文件:“+message”);
mh.处理消息(消息);
}*/
}
捕获(例外e)
{
e、 printStackTrace();
}
}

当我打印“retrievedmsg”变量时,它被打印为“String retrievedmsg=new String(bMsg,StandardCharsets.UTF_8)

有许多方法可以将字节解释为文本。每个在字节和文本之间转换的方案称为

当您写入
新字符串(bMsg)
时,您正在使用系统的默认字符集转换bMsg的字节。您收到的字节似乎不在该字符集中

每当您尝试在字节和文本之间进行转换时,您都需要知道字符集。使用
新字符串(byte[])
并不能解决问题,它只是意味着您忽略了这个问题,这将给您带来风险,正如您在本例中看到的那样

我猜您系统的默认字符集是一个单字节字符集,如ISO-8859-1或windows-1252,它期望一个字节代表一个字符,但您收到的文本是little endian UTF-16,它使用两个字节代表每个字符。如果是这种情况,您可以通过指定该字符集来解决它:

String retrievedmsg = new String(bMsg, StandardCharsets.UTF_16LE);
但是,单凭这一点并不能解决问题。您需要从消息提供商那里了解他们是否总是以UTF-16LE格式发送消息。如果不知道用于传输数据的字符集,您就无法知道如何将字节转换为文本

MQEnvironment.hostname=“reese.int.westgroup.com”字符串 queueName=“COMSERV.SRCHEXT.EVENTS.PUBLISH.QA.Q01”

您应该始终使用虚拟值替换私有内部公司值

其次,您应该使用哈希表而不是MQEnvironment类

这看起来像Windows双字节aka字符集1252。这不是MQ问题,而是Java转换问题。您尝试了什么?是否尝试按照VGR或rsampaths16建议在“新字符串”上指定字符集

i、 e

最后,为什么不在十六进制转储中输出消息负载呢?这样您就可以确切地知道您正在处理什么

如果您不知道如何做到这一点,那么这里有一些示例Java代码可以为您做到这一点。将其放入您的工具包中,以便您可以轻松地将其添加到任何项目中

/**
 * Dump a byte array as a standard HEX dump output
 * @param data the entire byte array
 * @param widthSize width of the HEX dump. Must be in multiples of 16
 */
public void dumpBuffer(byte[] data, int widthSize)
{
   int endPt = widthSize;
   int len = data.length;
   byte[] tempB = new byte[widthSize];

   if (widthSize == 16)
   {
      System.out.println("Address  0 1 2 3  4 5 6 7  8 9 A B  C D E F  0123456789ABCDEF");
      System.out.println("------- -------- -------- -------- --------  ----------------");
   }

   for (int i=0; i < len; i+=widthSize)
   {
      if (i+widthSize >= len)
         endPt = len - i;

      for (int j=0; j < endPt; j++)
         tempB[j] = data[i+j];

      System.out.println(formatHex(tempB, (i+widthSize < len?widthSize:len-i), i, widthSize ));
   }
}

/**
 * Format an array of bytes into a hex display
 * @param src a portion of the byte array
 * @param len length of this part of the byte array
 * @param index location of current position in data
 * @param width width of the HEX dump
 * @return
 */
private String formatHex(byte[] src, int lenSrc, int index, int width)
{
   String HEX = "0123456789ABCDEF";
   int i,j;
   int g = width / 4; /* number of groups of 4 bytes */
   int d = g * 9;     /* hex display width */
   StringBuffer sb = new StringBuffer();

   if ( (src == null) ||
        (lenSrc < 1) || (lenSrc > width) ||
        (index < 0) ||
        (g % 4 != 0) ||   /* only allow width of 16 / 32 / 48 etc. */
        (d < 36) )
   {
      return "";
   }

   String hdr = Integer.toHexString(index);
   if (hdr.length() <= 6)
      sb.append("000000".substring(0, 6 - hdr.length()) + hdr + ": ");
   else
      sb.append(hdr + ": ");

   /* hex display 4 by 4 */
   for(i=0; i < lenSrc; i++)
   {
      sb.append(""+HEX.charAt((src[i]) >> 4) + HEX.charAt((src[i] & 0x0F)));

      if (((i+1) % 4) == 0)
         sb.append(" ");
   }

   /* blank fill hex area if we do not have "width" bytes */
   if (lenSrc < width)
   {
      j = d - (lenSrc*2) - (lenSrc / 4);
      for(i=0; i < j; i++)
         sb.append(" ");
   }

   /* character display */
   sb.append(" ");
   for (i=0; i < lenSrc; i++)
   {
      if(Character.isISOControl((char)src[i]))
         sb.append(".");
      else
         sb.append((char)src[i]);
   }

   /* blank fill character area if we do not have "width" bytes */
   if (lenSrc < width)
   {
      j = width - lenSrc;
      for(i=0; i < j; i++)
         sb.append(" ");
   }

   return sb.toString();
}
其中bMsg是从队列中检索到的代码的消息。十六进制转储类似于:

地址0 1 2 3 4 5 6 7 8 9 A B C D E F 0123456789ABCDEF
------- -------- -------- -------- --------  ----------------
000000:54686973 20697320 61207465 7374206D这是一个测试m
000010:65737361 67652061 6E642061 6E6F7468信息和其他
000020:65722065 7863656C 6C656E74 20657861 er优秀exa
000030:6D706C65 20627920 526F6765 722C2069样本由i.罗杰提供
000040:66204920 646F2073 61792073 6F206D79 f我确实这么说,我的
000050:73656C66 21212020 3A29自我!!:)

您正在将其转换为一个字节,然后再转换回一个字符串?消息是
/**
 * Dump a byte array as a standard HEX dump output
 * @param data the entire byte array
 * @param widthSize width of the HEX dump. Must be in multiples of 16
 */
public void dumpBuffer(byte[] data, int widthSize)
{
   int endPt = widthSize;
   int len = data.length;
   byte[] tempB = new byte[widthSize];

   if (widthSize == 16)
   {
      System.out.println("Address  0 1 2 3  4 5 6 7  8 9 A B  C D E F  0123456789ABCDEF");
      System.out.println("------- -------- -------- -------- --------  ----------------");
   }

   for (int i=0; i < len; i+=widthSize)
   {
      if (i+widthSize >= len)
         endPt = len - i;

      for (int j=0; j < endPt; j++)
         tempB[j] = data[i+j];

      System.out.println(formatHex(tempB, (i+widthSize < len?widthSize:len-i), i, widthSize ));
   }
}

/**
 * Format an array of bytes into a hex display
 * @param src a portion of the byte array
 * @param len length of this part of the byte array
 * @param index location of current position in data
 * @param width width of the HEX dump
 * @return
 */
private String formatHex(byte[] src, int lenSrc, int index, int width)
{
   String HEX = "0123456789ABCDEF";
   int i,j;
   int g = width / 4; /* number of groups of 4 bytes */
   int d = g * 9;     /* hex display width */
   StringBuffer sb = new StringBuffer();

   if ( (src == null) ||
        (lenSrc < 1) || (lenSrc > width) ||
        (index < 0) ||
        (g % 4 != 0) ||   /* only allow width of 16 / 32 / 48 etc. */
        (d < 36) )
   {
      return "";
   }

   String hdr = Integer.toHexString(index);
   if (hdr.length() <= 6)
      sb.append("000000".substring(0, 6 - hdr.length()) + hdr + ": ");
   else
      sb.append(hdr + ": ");

   /* hex display 4 by 4 */
   for(i=0; i < lenSrc; i++)
   {
      sb.append(""+HEX.charAt((src[i]) >> 4) + HEX.charAt((src[i] & 0x0F)));

      if (((i+1) % 4) == 0)
         sb.append(" ");
   }

   /* blank fill hex area if we do not have "width" bytes */
   if (lenSrc < width)
   {
      j = d - (lenSrc*2) - (lenSrc / 4);
      for(i=0; i < j; i++)
         sb.append(" ");
   }

   /* character display */
   sb.append(" ");
   for (i=0; i < lenSrc; i++)
   {
      if(Character.isISOControl((char)src[i]))
         sb.append(".");
      else
         sb.append((char)src[i]);
   }

   /* blank fill character area if we do not have "width" bytes */
   if (lenSrc < width)
   {
      j = width - lenSrc;
      for(i=0; i < j; i++)
         sb.append(" ");
   }

   return sb.toString();
}
dumpBuffer(bMsg, 16);