Is there a way to convert a.netty ByteBuf to a String and vice versa?
public String toString(ByteBuf b){
//return b enconded to a String
}
public Bytebuf ToByteBuff(String s){
//return s decoded to Bytebuf
}
You can use ByteBuf.toString(Charset)
to convert to string.
You can use String.getBytes(Charset)
and Unpooled.wrappedBuffer(byte[])
to convert to ByteBuf.
您可以使用Unpooled.copiedBuffer(String, Charset)
从字符串创建ByteBuf
。
To convert ByteBuf to String, it needs:
public void channelRead(ChannelHandlerContext ctx, Object msg) {
ByteBuf in = (ByteBuf) msg;
String clientMessage = in.toString(CharsetUtil.UTF_8);
To convert String to ByteBuf, it needs:
String message = "text";
ByteBuf in = Unpooled.wrappedBuffer(message.getBytes());
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.