ByteBufに文字列
文字列型のバイトに1)を使用するString.getBytes(文字セット)、[]
2)使用Unpooled.wrappedBuffer(byte[]),将byte[]转为ByteBuf
文字列MSG =「メッセージ」。 バイト []バイト= msg.getBytes(CharsetUtil.UTF_8)。 ByteBuf BUF = Unpooled.wrappedBuffer(バイト)。
または使用Unpooled.copiedBuffer(CharSequence引数の文字列、文字セットの文字セット)
ByteBuf BUF = Unpooled.copiedBuffer( "こんにちは"、CharsetUtil.UTF_8)。
文字列へのByteBuf
使用ByteBuf.toString(Charset),将ByteBuf转为String
buf.toString(CharsetUtil.UTF_8)
例:
文字列MSG =「メッセージ」。 バイト []バイト= msg.getBytes(CharsetUtil.UTF_8)。 ByteBuf BUF = Unpooled.wrappedBuffer(バイト)。 System.out.println(buf.toString(CharsetUtil.UTF_8))。
出力:メッセージ