Java 类型转换:byte数组和其他类型转换

47 篇文章 0 订阅
// 字节转成UCHAR型,由于Java不支持UCHAR类型,所以这里转换成short类型
    public static short byte2Uchar(byte[] b, int off) {
        int temp = b[0];
        if (b[0] < 0) {
            temp = temp + 256;
        }
        return (short) temp;
    }

    // 字节转成short型
    public static short byte2Short(byte[] b, int off) {
        return (short) (((b[off + 1] & 0xFF) << 0) + ((b[off + 0] & 0xFF) << 8));
    }

    // 字节转成int型
    public static int byte2Int(byte[] b, int off) {
        return ((b[off + 3] & 0xFF) << 0) + ((b[off + 2] & 0xFF) << 8)
                + ((b[off + 1] & 0xFF) << 16) + ((b[off + 0] & 0xFF) << 24);
    }

    // 字节转成long型
    public static long byte2Long(byte[] b, int off) {
        return ((b[off + 7] & 0xFFL) << 0) + ((b[off + 6] & 0xFFL) << 8)
                + ((b[off + 5] & 0xFFL) << 16) + ((b[off + 4] & 0xFFL) << 24)
                + ((b[off + 3] & 0xFFL) << 32) + ((b[off + 2] & 0xFFL) << 40)
                + ((b[off + 1] & 0xFFL) << 48) + ((b[off + 0] & 0xFFL) << 56);
    }


    // 字节转成float型
    public static float byte2Float(byte[] b, int off) {
        int i = ((b[off + 3] & 0xFF) << 0) + ((b[off + 2] & 0xFF) << 8)
                + ((b[off + 1] & 0xFF) << 16) + ((b[off + 0] & 0xFF) << 24);
        return Float.intBitsToFloat(i);
    }


    // 字节转成double型
    public static double byte2Double(byte[] b, int off) {
        long j = ((b[off + 7] & 0xFFL) << 0) + ((b[off + 6] & 0xFFL) << 8)
                + ((b[off + 5] & 0xFFL) << 16) + ((b[off + 4] & 0xFFL) << 24)
                + ((b[off + 3] & 0xFFL) << 32) + ((b[off + 2] & 0xFFL) << 40)
                + ((b[off + 1] & 0xFFL) << 48) + ((b[off + 0] & 0xFFL) << 56);
        return Double.longBitsToDouble(j);
    }


    // 字节转成char型
    public static char byte2Char(byte[] b, int off) {
        return (char) (((b[off + 1] & 0xFF) << 0) + ((b[off + 0] & 0xFF) << 8));
    }

    // 长整型转成字节
    public static byte[] longToByte(long number) {
        long temp = number;
        byte[] b = new byte[8];
        for (int i = 0; i < b.length; i++) {
            b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位 
            temp = temp >> 8; // 向右移8位 
        }
        return b;
    }
        String charsetName = "UTF-8";
        String stringContent = "Test";
        String string = new String(stringContent);
        byte[] bytes = new byte[0];
        try {
            // 字符串转字节
            bytes = string.getBytes(charsetName);
            System.out.println(bytes);
            for(byte b : bytes){
                System.out.println(b);
            }
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        try {
            // 字节转字符串
            String newString = new String(bytes, charsetName);
            System.out.println(newString);
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

当然,借助java.io里的CharBuffer、IntBuffer等,也是一种选择,可参考:https://blog.csdn.net/nie_feilong/article/details/5784595

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

风铃峰顶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值