码迷,mamicode.com
首页 > 编程语言 > 详细

Java中byte[]和char[]互相转换

时间:2019-03-28 15:15:39      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:string   str   void   buffer   encode   new   auth   package   flip   

package gj;

import java.nio.ByteBuffer;
import java.nio.CharBuffer;
import java.nio.charset.Charset;
import java.util.Arrays;

/**
 * Author: areful
 * Date: 2019/3/28
 */
public class CharByteKit {
    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();
    }

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

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

    public static char byteToChar(byte[] b) {
        int hi = (b[0] & 0xFF) << 8;
        int lo = b[1] & 0xFF;
        return (char) (hi | lo);
    }

    public static void main(String[] args) {
        // char[] <===> byte[]
        char[] c = getChars(new byte[]{65, 2, 3});
        System.out.println(Arrays.toString(c));
        byte[] b = getBytes(c);
        System.out.println(Arrays.toString(b));

        // char <===> byte[]
        byte[] b2 = charToByte(‘A‘);
        System.out.println(Arrays.toString(b2));
        char c2 = byteToChar(b2);
        System.out.println(c2);
    }
}

  

Java中byte[]和char[]互相转换

标签:string   str   void   buffer   encode   new   auth   package   flip   

原文地址:https://www.cnblogs.com/areful/p/10615091.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!