java 基礎型別與byte[]的轉換

weixin_34402408發表於2018-06-09

java的基礎型別(char、short、int、float、double、string)和byte[]的相互轉換工具類

import java.nio.charset.Charset;

/**
 * Created by wenshao on 2018/6/8.
 * java常用型別的bit大小及範圍
 * |  type   |  bit   |    min                              |   max                                  |
 * |  char   |  16     |    0                               |   2^8=65535                            |
 * |  short  |  16    |    -2^15=-32768                     |   2^15-1=32767                         |
 * |  int    |  32    |    -2^31=-2147483648                |   2^31-1=2147483647                    |
 * |  long   |  64    |    -2^63=-9223372036854775808       |   2^63-1=9223372036854775807           |
 * |  float  |  32    |    -2^149=1.4E-45                   |   2^128-1=3.4028235E38                 |
 * |  double |  64    |    -2^1074=4.9E-324                 |   2^1024-1=1.7976931348623157E308      |
 * ========================================
 * 8個bit為的最大為255, 二進位制表示為1111 1111, 十六進位制表示為0xff
 * 比如short 100, 轉換成二進位制0000 0000 0110 0100
 * ========================================
 * 原碼、反碼和補碼
 * 正數的原碼、反碼和補碼都是一樣。
 * 負數: 如:-100
 * 原碼:1110 0100  (最高位為符號位 1 負數 0 正數)
 * 反碼:1001 1011
 * 補碼:1001 1100
 * ===============================
 * java byte儲存為補碼
 * ================================
 * type & 0xff意義: 去除除了最後8位的其餘高位,只獲取最後8位。
 * 型別轉換
 */
public class TypeConvert {

    public static byte[] charToBytes(char data) {
        return new byte[]{
                (byte) (data & 0xff),
                (byte) ((data >> 8) & 0xff)
        };
    }

    public static char bytesToChar(byte[] bytes) {
        return (char) (bytes[0] & 0xFF | (bytes[1] & 0xFF) << 8);
    }

    public static byte[] shortToBytes(short data) {
        return new byte[]{
                (byte) (data & 0xff),
                (byte) ((data >> 8) & 0xff)
        };
    }

    public static short bytesToShort(byte[] bytes) {
        return (short) (bytes[0] & 0xFF | (bytes[1] & 0xFF) << 8);
    }

    public static byte[] intToBytes(int data) {
        return new byte[]{
                (byte) (data & 0xff),
                (byte) ((data >> 8) & 0xff),
                (byte) ((data >> 16) & 0xff),
                (byte) ((data >> 24) & 0xff),
        };
    }

    public static int bytesToInt(byte[] bytes) {
        return (bytes[0] & 0xFF) |
                ((bytes[1] & 0xFF) << 8) |
                ((bytes[2] & 0xFF) << 16) |
                ((bytes[3] & 0xFF) << 24);
    }

    public static byte[] longToBytes(long data) {
        return new byte[]{
                (byte) (data & 0xff),
                (byte) ((data >> 8) & 0xff),
                (byte) ((data >> 16) & 0xff),
                (byte) ((data >> 24) & 0xff),
                (byte) ((data >> 32) & 0xff),
                (byte) ((data >> 40) & 0xff),
                (byte) ((data >> 48) & 0xff),
                (byte) ((data >> 56) & 0xff),
        };
    }

    public static long bytesToLong(byte[] bytes) {
        return ((long) bytes[0] & 0xFF) |
                ((long) (bytes[1] & 0xFF) << 8) |
                ((long) (bytes[2] & 0xFF) << 16) |
                ((long) (bytes[3] & 0xFF) << 24) |
                ((long) (bytes[4] & 0xFF) << 32) |
                ((long) (bytes[5] & 0xFF) << 40) |
                ((long) (bytes[6] & 0xFF) << 48) |
                ((long) (bytes[7] & 0xFF) << 56);
    }

    public static byte[] floatToBytes(float data) {
        int intBits = Float.floatToIntBits(data);
        return intToBytes(intBits);
    }

    public static float bytesToFloat(byte[] bytes) {
        return Float.intBitsToFloat(bytesToInt(bytes));
    }

    public static byte[] doubleToBytes(double data) {
        long longBits = Double.doubleToLongBits(data);
        return longToBytes(longBits);
    }

    public static double bytesToDouble(byte[] bytes) {
        long l = bytesToLong(bytes);
        return Double.longBitsToDouble(l);
    }

    public static byte[] stringToBytes(String data, String charsetName) {
        Charset charset = Charset.forName(charsetName);
        return data.getBytes(charset);
    }
    public static byte[] stringToBytes(String data) {
        return stringToBytes(data, "UTF-8");
    }
    public static String bytesToString(byte[] bytes, String charsetName) {
        return new String(bytes, Charset.forName(charsetName));
    }
    public static String bytesToString(byte[] bytes) {
        return bytesToString(bytes, "UTF-8");
    }

}

相關文章