如何在java中實現一個無符號字節,限制的大小應該只有1個字節,即我們不能通過And'轉換爲short或int'使用0xFF如何在java中實現一個無符號字節,限制的大小應該只有1個字節
我必須通過套接字傳輸一個無符號字節數組,因爲接收結束是一個C代碼方法期待無符號字節數組的大小爲1字節,但由於Java不支持無符號字節的概念, 。我們是否有任何方法來實現這一點。
字節b =(字節)0xF0;甚至字節b1 = 0x00;沒有通過套接字通道正確發送。請參閱寫入服務器的方法。
公共無效編碼(IoSession會話,對象信息,ProtocolEncoderOutput出) 拋出異常{
//lProxylogger.info("Inside ProtocolEncoderAdapter. Encoding response to client..");
String response ;
//lProxylogger.info("The response length in encode adapter is "+ response.length()+" Message="+response);
byte[] responseStream;
response=(String) message;
responseStream= response.getBytes("windows-1252");
//responseStream=serialize(message);
IoBuffer buffer = IoBuffer.allocate(responseStream.length);
// buffer.putInt(response.length);
//IoBuffer buffer=(IoBuffer)message;
// buffer.putObject(message);
System.out.println("Encoded Response:"+new String(responseStream));
for(byte b:responseStream)
System.out.print(b+",");
System.out.println();
buffer.put(responseStream);
// lProxylogger.info("the response buffer size is "+ buffer.capacity());
buffer.setAutoShrink(true);
buffer.shrink();
// lProxylogger.info("After shrinking the buffer size is "+ buffer.capacity());
buffer.flip();
//System.out.println("Writing response to Stream..");
out.write(buffer);
}
我認爲問題在於如何在C端打印出二進制文件,請參閱我的答案。 – 2013-04-08 07:10:33