如何在java中实现一个无符号字节,限制是大小只能是1个字节,即我们不能通过And'ing与0xFF转换为short或int
我必须通过套接字传输一个无符号字节数组,因为接收端是一个 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);
}