如何将netty ByteBuf转换为String,反之亦然

Kal*_*lue 7 java encoding decoding netty

有没有办法将netty ByteBuf转换为String,反之亦然?

public String toString(ByteBuf b){

 //return b enconded to a String
}

public Bytebuf ToByteBuff(String s){

  //return s decoded to Bytebuf
}
Run Code Online (Sandbox Code Playgroud)

Chr*_*ole 14

您可以使用ByteBuf.toString(Charset)转换为字符串.

您可以使用String.getBytes(Charset)Unpooled.wrappedBuffer(byte[])转换为ByteBuf.