在项目开发中,经常使用到对字节的转换,这里整理了,对于各种基本类型以及引用类型String的转换静态方法。提高了我们开发的进度,优雅的处理了我们的代码,让更多的精力开发放在业务上面。
    文章开头.jpg

    整理的方法概括:

    1. 1. short转换为byte[]
    2. 1. char转换为byte[]
    3. 1. int转化为byte[]
    4. 1. long转换为byte[]
    5. 1. float转换为byte[]
    6. 1. double转换为byte[]
    7. 1. String+charsetName转换为byte[]
    8. 1. String+GBK转换为byte[]
    9. 1. byte[]转换为short
    10. 1. byte[]转换为char
    11. 1. byte[]转换为int
    12. 1. byte[]转换为long
    13. 1. byte[]转换为float
    14. 1. byte[]转换为double
    15. 1. byte[]+charsetName转换为String
    16. 1. byte[] + GBK转换为String
    17. 1. String转化为HexString
    18. 1. byte[]转化为HexString
    19. 1. HexStriing转化为String无需Unicode解码
    20. 1. short转换为HexString

    工具类ByteUtil代码如下:

    package com.data_acquisition_system.demo.util;
    
    import java.io.ByteArrayOutputStream;
    import java.nio.charset.Charset;
    
    /**
     * @author : [王振宇]
     * @version : [v1.0]
     * @className : ByteUtil
     * @description : [字节工具类]
     * @createTime : [2021/8/6 11:59]
     * @updateUser : [王振宇]
     * @updateTime : [2021/8/6 11:59]
     * @updateRemark : [添加方法注释]
     */
    public final class ByteUtil {
    
        /**
         * short 转换为 byte[]
         * @param data short
         * @return byte[]
         */
        public static byte[] getBytes(short data) {
            byte[] bytes = new byte[2];
            bytes[0] = (byte) (data & 0xff);
            bytes[1] = (byte) ((data & 0xff00) >> 8);
            return bytes;
        }
    
        /**
         * char 转换为 byte[]
         * @param data char
         * @return  byte[]
         */
        public static byte[] getBytes(char data) {
            byte[] bytes = new byte[2];
            bytes[0] = (byte) (data);
            bytes[1] = (byte) (data >> 8);
            return bytes;
        }
    
        /**
         * int 转换为 byte[]
         * @param data int
         * @return byte[]
         */
        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;
        }
    
        /**
         * long 转换为 byte[]
         * @param data long
         * @return byte[]
         */
        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;
        }
    
        /**
         * float 转换为 byte[]
         * @param data float
         * @return byte[]
         */
        public static byte[] getBytes(float data) {
            int intBits = Float.floatToIntBits(data);
            return getBytes(intBits);
        }
    
        /**
         * double 转换 byte[]
         * @param data double
         * @return byte[]
         */
        public static byte[] getBytes(double data) {
            long intBits = Double.doubleToLongBits(data);
            return getBytes(intBits);
        }
    
        /**
         *  string 类型 转换 byte[]
         * @param data string
         * @param charsetName string 编码集
         * @return byte[]
         */
        public static byte[] getBytes(String data, String charsetName) {
            Charset charset = Charset.forName(charsetName);
            return data.getBytes(charset);
        }
    
        /**
         *  string 类型 编码 GBK 转换 byte[]
         * @param data string
         * @return byte[]
         */
        public static byte[] getBytes(String data) {
            return getBytes(data, "GBK");
        }
    
        /**
         * byte[] 转化为 short
         * @param bytes byte[]
         * @return short
         */
        public static short getShort(byte[] bytes) {
            return (short) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
        }
    
        /**
         * byte[] 转化为 char
         * @param bytes byte[]
         * @return char
         */
        public static char getChar(byte[] bytes) {
            return (char) ((0xff & bytes[0]) | (0xff00 & (bytes[1] << 8)));
        }
    
        /**
         * byte[] 转化为 int
         * @param bytes byte[]
         * @return int
         */
        public static int getInt(byte[] bytes) {
            return (0xff & (int) bytes[0]) |
                    (0xff00 & ((int) bytes[1] << 8)) |
                    (0xff0000 & ((int) bytes[2] << 16)) |
                    (0xff000000 & ((int) bytes[3] << 24));
        }
    
        /**
         *  byte[] 转化为 long
         * @param bytes byte[]
         * @return long
         */
        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));
        }
    
        /**
         * byte[] 转化为 float
         * @param bytes byte[]
         * @return float
         */
        public static float getFloat(byte[] bytes) {
            return Float.intBitsToFloat(getInt(bytes));
        }
    
        /**
         * byte[] 转化为 double
         * @param bytes byte[]
         * @return double
         */
        public static double getDouble(byte[] bytes) {
            long l = getLong(bytes);
            //System.out.println(l);
            return Double.longBitsToDouble(l);
        }
    
        /**
         * byte[] 转化为 string
         * @param bytes byte[]
         * @param charsetName 编码
         * @return string
         */
        public static String getString(byte[] bytes, String charsetName) {
            return new String(bytes, Charset.forName(charsetName));
        }
    
        /**
         * byte[] 转化为 GBK 编码的 String
         * @param bytes byte
         * @return String
         */
        public static String getString(byte[] bytes) {
            return getString(bytes, "GBK");
        }
    
        /**
         * 字符串转化为16进制的字符串
         * @param str 字符串
         * @return 16进制字符串
         */
        public static String strToHex(String str) {
            String hexString = "0123456789ABCDEF";
            byte[] bytes = str.getBytes();
            StringBuilder hex = new StringBuilder(bytes.length * 2);
            for (int i = 0; i < bytes.length; i++) {
                hex.append(hexString.charAt((bytes[i] & 0xf0) >> 4));  // 作用同 n / 16
                hex.append(hexString.charAt((bytes[i] & 0x0f) >> 0));  // 作用同 n
                hex.append(' ');  //中间用空格隔开
            }
            return hex.toString();
        }
    
        /**
         * byte[]转16进制Str
         * @param byteArray byte[]
         * @return string
         */
        public static String ByteArrayToHexStr(byte[] byteArray) {
            if (byteArray == null)
                return null;
            char[] hexArray = "0123456789ABCDEF".toCharArray();
            char[] hexChars = new char[byteArray.length * 2];
            for (int i = 0; i < byteArray.length; i++) {
                int temp = byteArray[i] & 0xFF;
                hexChars[i * 2] = hexArray[temp >>> 4];
                hexChars[i * 2 + 1] = hexArray[temp & 0x0F];
            }
            return new String(hexChars);
        }
    
        /**
         * 16进制直接转换成为字符串(无需Unicode解码)
         * @param hexStr  16进制字符串
         * @return 字符串
         */
        public static String hexStr2Str(String hexStr) {
            String str = "0123456789ABCDEF";
            char[] hexs = hexStr.toCharArray();
            byte[] bytes = new byte[hexStr.length() / 2];
            int n;
            for (int i = 0; i < bytes.length; i++) {
                n = str.indexOf(hexs[2 * i]) * 16;
                n += str.indexOf(hexs[2 * i + 1]);
                bytes[i] = (byte) (n & 0xff);
            }
            return new String(bytes);
        }
    
        /**
         * short 转换 16进制 String
         * @param data short
         * @return string
         */
        public static String byte2HexStr(short data) {
            String str = "0123456789ABCDEF";
            char[] chs = new char[2];
            data &= 0x0FF;
            chs[0] = str.charAt(data / 16);
            chs[1] = str.charAt(data & 0x0F);
            return new String(chs);
        }
    
        public static void main(String[] args) {
            System.out.println(byte2HexStr((byte) 28));
        }
    
    }