我这面做服务器接收 客户端发来的16进制 字节码,如图(图片是客户端发来的)我想原模原样的接收到这些字节码,返回一个字节数组
channelRead( ... ){
ByteBuf buf = (ByteBuf) msg;
byte[] receiveMsgBytes = new byte[buf.readableBytes()];
buf.readBytes(receiveMsgBytes);
//receiveMsgBytes 就收到了.
}
如果还想转换成16进制字符串
maven的pom中加入
<dependency>
<groupId>commons-codec</groupId>
<artifactId>commons-codec</artifactId>
<version>1.10</version>
</dependency>
就可以用Hex.encodeHexString(receiveMsgBytes)
这个转换那当然也可以自己写
public int read(byte b[], int off, int len) throws IOException
看 api
接收到的都是byte[],要自己将byte[]转成16进制
public static String bytesToHexString(byte[] src) {
StringBuffer sb = new StringBuffer("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v).toUpperCase();
if (hv.length() < 2) {
sb.append(0);
}
sb.append(hv);
if (i != src.length - 1) {
sb.append(" ");
}
}
return sb.toString();
}
如果还想转换成16进制字符串
maven的pom中加入
commons-codec
commons-codec
1.10
啊啊啊
netty是一个异步NIO网络应用程序框架,可以自定义加解码器类(channelhandler子类)添加到channelpipeline里。
传输过来的数据:A1DD0ABC2 接受后:in.readbyte A1, in.readbyte D0 , in.readbyte BC 这是怎么回事