zoukankan      html  css  js  c++  java
  • Java中char与byte的互转

    char[] 数组转为byte[] 数组

    public static byte[] getBytes(char[] chars) {
    Charset cs = Charset.forName("UTF-8");
    CharBuffer cb = CharBuffer.allocate(chars.length);
    cb.put(chars);
    cb.flip();
    ByteBuffer bb = cs.encode(cb);
    return bb.array();
    }


    byte[] 数组转为数组 char[]

    public static char[] getChars(byte[] bytes) {
    Charset cs = Charset.forName("UTF-8");
    ByteBuffer bb = ByteBuffer.allocate(bytes.length);
    bb.put(bytes);
    bb.flip();
    CharBuffer cb = cs.decode(bb);
    return cb.array();
    }

    char 转 byte[] 数组

    public static byte[] charToByte(char c) {
    byte[] b = new byte[2];
    b[0] = (byte) ((c & 0xFF00) >> 8);
    b[1] = (byte) (c & 0xFF);
    return b;
    }

    byte[] 数组转 char

    public static char byteToChar(byte[] b) {
    char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));
    return c;
    }
    ————————————————
    原文链接:https://blog.csdn.net/aouixh/java/article/details/80662400

  • 相关阅读:
    eclipse使用svn
    yum安装mysql
    spring中aop使用
    mybatis定义拦截器
    横扫页面的三大标签
    springmvc日期格式化
    springmvc笔记
    springboot跳转jsp页面
    常用网址
    CentOS Android Studio桌面图标的创建
  • 原文地址:https://www.cnblogs.com/lwh-12345/p/12979292.html
Copyright © 2011-2022 走看看