zoukankan      html  css  js  c++  java
  • netty ByteBuf与String相互转换

    String转为ByteBuf

    1)使用String.getBytes(Charset),将String转为byte[]类型

    2)使用Unpooled.wrappedBuffer(byte[]),将byte[]转为ByteBuf

            String msg = "A message";
            byte[] bytes = msg.getBytes(CharsetUtil.UTF_8);
            ByteBuf buf = Unpooled.wrappedBuffer(bytes);

    或者使用 Unpooled.copiedBuffer(CharSequence string, Charset charset)

    ByteBuf buf = Unpooled.copiedBuffer("Hello", CharsetUtil.UTF_8);

    ByteBuf转为String

    使用ByteBuf.toString(Charset),将ByteBuf转为String

    buf.toString(CharsetUtil.UTF_8)

    示例:

            String msg = "A message";
            byte[] bytes = msg.getBytes(CharsetUtil.UTF_8);
            ByteBuf buf = Unpooled.wrappedBuffer(bytes);
            System.out.println(buf.toString(CharsetUtil.UTF_8));

    输出:A message

  • 相关阅读:
    System 类的使用
    StringBuffer 与 StringBuilder类的使用
    String 类 的 使用
    多线程
    音乐播放
    数据库
    表示图编辑
    UITextView(2)
    UITextView
    tarBar
  • 原文地址:https://www.cnblogs.com/deltadeblog/p/11464948.html
Copyright © 2011-2022 走看看