HexUtils工具类,支持字节各种转换


import java.io.*;
import java.text.SimpleDateFormat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * 字节转换工具类
 */
public class HexUtils {

    //10进制转十六进制
    // 低字节在前(低字节序)
    public static byte[] toLH(int n) {
        byte[] b = new byte[4];
        b[0] = (byte) (n & 0xff);
        b[1] = (byte) (n >> 8 & 0xff);
        b[2] = (byte) (n >> 16 & 0xff);
        b[3] = (byte) (n >> 24 & 0xff);
        return b;
    }

    //10进制转十六进制
    //高字节在前(高字节序)
    public static byte[] toHH(int n) {
        byte[] b = new byte[4];
        b[3] = (byte) (n & 0xff);
        b[2] = (byte) (n >> 8 & 0xff);
        b[1] = (byte) (n >> 16 & 0xff);
        b[0] = (byte) (n >> 24 & 0xff);
        return b;
    }

    //byte[]转String
    public static String bytesToString(byte[] value){
        StringBuilder stringBuilder=new StringBuilder(value.length*2);
        for(byte b:value)
        {
            stringBuilder.append(String.format("%02X",new Integer(b & 0xFF)));
        }

        return stringBuilder.toString();
    }

    //打印byte[]
    public static void printHexString( byte[] b) {
        SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        for (int i = 0; i < b.length; i++) {
            String hex = Integer.toHexString(b[i] & 0xFF);
            if (hex.length() == 1) {
                hex = '0' + hex;
            }
            System.out.print(hex.toUpperCase()+" , ");
        }
        System.out.println();
    }

    //byte[]转double
    public static double byteArrayToDouble(byte[] bytes){
        long ff = 0xFF;
        long bitLayoutLongValue = 0;
        for (int i = 0; i < bytes.length; i++) {
            bitLayoutLongValue |= (bytes[i] & ff) << (bytes.length - i - 1) * 8;
        }
        return Double.longBitsToDouble(bitLayoutLongValue);

    }

    //byte[]转file
    public static void byteArrayToFile(byte[] src, File dest) {
        OutputStream os = null;
        try {
            os = new BufferedOutputStream(new FileOutputStream(dest));
            InputStream is = new ByteArrayInputStream(src);
            byte[] flushBuffer = new byte[src.length];
            int readLen = -1;
            while ((readLen = is.read(flushBuffer)) != -1) {
                os.write(flushBuffer, 0, readLen);
            }
            os.flush();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (os != null) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }
    /**
     * byte数组转int类型
     */
    public static int byteArrayToInt(byte[] bytes) {
        int ff = 0xFF;
        int value = 0;
        for (int i = 0; i < bytes.length; i++) {
            value |= (bytes[i] & ff) << (bytes.length - i - 1) * 8;
        }
        return value;

    }
   
    /**
     * byte数组转float类型
     */
    public static float byteArrayToFloat(byte[] b) {
        return Float.intBitsToFloat(Integer.valueOf(bytesToString(b).trim(), 16));
    }
    
    /**
     * String转byte数组
     */
    public static byte[] toByteArray(String hexString) {

        hexString = hexString.toLowerCase();
        final byte[] byteArray = new byte[hexString.length() / 2];
        int k = 0;

        for (int i = 0; i < byteArray.length; i++) {   //因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
            byteArray[i] = (byte) (high << 4 | low);
            k += 2;
        }

        return byteArray;
    }
    /**
     * byte数组中取int数值,本方法适用于(低位在前,高位在后)的顺序,和和intToBytes()配套使用
     */
    public static int bytesToInt(byte[] src, int offset) {
        int value;
        value = (int) ((src[offset] & 0xFF)
                | ((src[offset+1] & 0xFF)<<8)
                | ((src[offset+2] & 0xFF)<<16)
                | ((src[offset+3] & 0xFF)<<24));
        return value;
    }

    /**
     * byte数组中取int数值,本方法适用于(低位在后,高位在前)的顺序。和intToBytes2()配套使用
     */
    public static int bytesToInt2(byte[] src, int offset) {
        int value;
        value = (int) ( ((src[offset] & 0xFF)<<24)
                |((src[offset+1] & 0xFF)<<16)
                |((src[offset+2] & 0xFF)<<8)
                |(src[offset+3] & 0xFF));
        return value;
    }


}

HexUtils工具类,支持byte数组和Integer、float、double、String、File各种类型转换

亲测有效

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值