簡體   English   中英

如何將 a.netty ByteBuf 轉換為 String,反之亦然

[英]How to convert a netty ByteBuf to a String and vice versa

有沒有辦法將 a.netty ByteBuf 轉換為 String,反之亦然?

public String toString(ByteBuf b){

 //return b enconded to a String
}

public Bytebuf ToByteBuff(String s){

  //return s decoded to Bytebuf
}

您可以使用ByteBuf.toString(Charset)轉換為字符串。

您可以使用Unpooled.wrappedBuffer(byte[]) String.getBytes(Charset)Unpooled.wrappedBuffer(byte[])轉換為ByteBuf。

您可以使用Unpooled.copiedBuffer(String, Charset)從字符串創建ByteBuf

要將 ByteBuf 轉換為 String,需要:

 public void channelRead(ChannelHandlerContext ctx, Object msg) {
        ByteBuf in = (ByteBuf) msg;
        String clientMessage = in.toString(CharsetUtil.UTF_8);

要將 String 轉換為 ByteBuf,需要:

 String message = "text";
 ByteBuf in = Unpooled.wrappedBuffer(message.getBytes());

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM