code: public static String toHex(ModbusMessage msg) {
          BytesOutputStream byteOutputStream = new BytesOutputStream(Modbus.MAX_MESSAGE_LENGTH);
          String ret = "-1";
          try {
              msg.writeTo(byteOutputStream);
              ret = toHex(byteOutputStream.getBuffer(), 0, byteOutputStream.size());
          }
          catch (IOException ex) {
              logger.debug("Hex conversion error {}", ex);
          }
          return ret;
      }
docs: Converts a <tt>ModbusMessage</tt> instance into
  a hex encoded string representation.
  
  @param msg the message to be converted.
  
  @return the converted hex encoded string representation of the message.