byte转各种类型工具

byte数据和基本类型之间相互转换工具类

public class ByteUtil {
    public static byte[] getBytes(short data) {

        byte[] bytes = new byte[2];

        bytes[0] = (byte) (data & 0xff);

        bytes[1] = (byte) ((data & 0xff00) >> 8);

        return bytes;

    }

    public static byte[] getBytes(char data) {

        byte[] bytes = new byte[2];

        bytes[0] = (byte) (data);

        bytes[1] = (byte) (data >> 8);

        return bytes;

    }

    public static byte[] getBytes(int data) {

        byte[] bytes = new byte[4];

        bytes[0] = (byte) (data & 0xff);

        bytes[1] = (byte) ((data & 0xff00) >> 8);

        bytes[2] = (byte) ((data & 0xff0000) >> 16);

        bytes[3] = (byte) ((data & 0xff000000) >> 24);

        return bytes;

    }

    public static byte[] getBytes(long data) {

        byte[] bytes = new byte[8];

        bytes[0] = (byte) (data & 0xff);

        bytes[1] = (byte) ((data >> 8) & 0xff);

        bytes[2] = (byte) ((data >> 16) & 0xff);

        bytes[3] = (byte) ((data >> 24) & 0xff);

        bytes[4] = (byte) ((data >> 32) & 0xff);

        bytes[5] = (byte) ((data >> 40) & 0xff);

        bytes[6] = (byte) ((data >> 48) & 0xff);

        bytes[7] = (byte) ((data >> 56) & 0xff);

        return bytes;

    }

    public static byte[] getBytes(float data) {

        int intBits = Float.floatToIntBits(data);

        return getBytes(intBits);

    }

    public static byte[] getBytes(double data) {

        long intBits = Double.doubleToLongBits(data);

        return getBytes(intBits);

    }

    public static byte[] getBytes(String data, String charsetName) {

        Charset charset = Charset.forName(charsetName);

        return data.getBytes(charset);

    }

    public static byte[] getBytes(String data) {

        return getBytes(data, "GBK");

    }

    public static short getShort(byte[] bytes) {

        return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));

    }

    public static char getChar(byte[] bytes) {

        return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));

    }

    public static int getInt(byte[] bytes) {

        return (0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)) | (0xff0000 & (bytes[2] << 16)) | (0xff000000 & (bytes[3] << 24));

    }

    public static long getLong(byte[] bytes) {

        return(0xffL & (long)bytes[0]) | (0xff00L & ((long)bytes[1] << 8)) | (0xff0000L & ((long)bytes[2] << 16)) | (0xff000000L & ((long)bytes[3] << 24))

                | (0xff00000000L & ((long)bytes[4] << 32)) | (0xff0000000000L & ((long)bytes[5] << 40)) | (0xff000000000000L & ((long)bytes[6] << 48)) | (0xff00000000000000L & ((long)bytes[7] << 56));

    }

    public static float getFloat(byte[] bytes) {

        return Float.intBitsToFloat(getInt(bytes));

    }

    public static double getDouble(byte[] bytes) {

        long l = getLong(bytes);

        return Double.longBitsToDouble(l);

    }

    public static String getString(byte[] bytes, String charsetName) {

        return new String(bytes, Charset.forName(charsetName));

    }

    public static String getString(byte[] bytes) {

        return getString(bytes, "GBK");

    }

    /**
     * 逆序
     * @param bytes1
     * @return
     */
    public static byte[] reverse(byte[] bytes1){
        byte[] bytes2 = new byte[bytes1.length];
        for (int i=0;i<bytes1.length;i++){
            bytes2[i] = bytes1[8-i-1];
        }
        return bytes2;
    }

    /**
     * double高低位转换
     * @param data
     * @return
     */
    public static double highLowReverse(double data){

        long l = Double.doubleToLongBits(data);

        byte[] bytes = new byte[8];

        bytes[7] = (byte) (l & 0xff);

        bytes[6] = (byte) ((l >> 8) & 0xff);

        bytes[5] = (byte) ((l >> 16) & 0xff);

        bytes[4] = (byte) ((l >> 24) & 0xff);

        bytes[3] = (byte) ((l >> 32) & 0xff);

        bytes[2] = (byte) ((l >> 40) & 0xff);

        bytes[1] = (byte) ((l >> 48) & 0xff);

        bytes[0] = (byte) ((l >> 56) & 0xff);

        return getDouble(bytes);
    }

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值