Java-byte转换

Java-byte转换

Java-byte转换
  1 import org.springframework.stereotype.Component;
  2 import org.springframework.util.StringUtils;
  3 
  4 import java.io.*;
  5 
  6 /**
  7  * byte和各种数据类型之间的转换
  8  *
  9  * @author zy
 10  * @date 2020-07-15 17:30
 11  */
 12 @Component
 13 public class ByteUtil {
 14 
 15     /**
 16      * 1、16进制转(字符串)byte
 17      *
 18      * @param hexString 16进制字符串
 19      * @return byte
 20      */
 21     public static byte[] hexStringToByte(String hexString) {
 22         //方法1
 23         if (StringUtils.isEmpty(hexString)) {
 24             return null;
 25         }
 26         hexString = hexString.toUpperCase();
 27         int length = hexString.length() / 2;
 28         char[] hexChars = hexString.toCharArray();
 29         byte[] d = new byte[length];
 30         for (int i = 0; i < length; i++) {
 31             int pos = i * 2;
 32             d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
 33         }
 34         return d;
 35         //方法2
 36 //        hexString = hexString.replaceAll(" ", "");
 37 //        int len = hexString.length();
 38 //        byte[] bytes = new byte[len / 2];
 39 //        for (int i = 0; i < len; i += 2) {
 40 //            // 两位一组,表示一个字节,把这样表示的16进制字符串,还原成一个字节
 41 //            bytes[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character
 42 //                    .digit(hexString.charAt(i + 1), 16));
 43 //        }
 44 //        return bytes;
 45         //方法3
 46 //        hexString = hexString.replaceAll(" ", "");
 47 //        final byte[] byteArray = new byte[hexString.length() / 2];
 48 //        int k = 0;
 49 //        for (int i = 0; i < byteArray.length; i++) {//因为是16进制,最多只会占用4位,转换成字节需要两个16进制的字符,高位在先
 50 //            byte high = (byte) (Character.digit(hexString.charAt(k), 16) & 0xff);
 51 //            byte low = (byte) (Character.digit(hexString.charAt(k + 1), 16) & 0xff);
 52 //            byteArray[i] = (byte) (high << 4 | low);
 53 //            k += 2;
 54 //        }
 55 //        return byteArray;
 56     }
 57 
 58     /**
 59      * 2、char转byte
 60      *
 61      * @param c 字符
 62      * @return byte
 63      */
 64     public static byte charToByte(char c) {
 65         return (byte) "0123456789ABCDEF".indexOf(c);
 66     }
 67 
 68     /**
 69      * 3、文件转byte
 70      *
 71      * @param filePath 文件路径
 72      * @return byte
 73      */
 74     public static byte[] file2byte(String filePath) {
 75         File tradeFile = new File(filePath);
 76         byte[] buffer = null;
 77         try {
 78             FileInputStream fis = new FileInputStream(tradeFile);
 79             ByteArrayOutputStream bos = new ByteArrayOutputStream();
 80             byte[] b = new byte[1024];
 81             int n;
 82             while ((n = fis.read(b)) != -1) {
 83                 bos.write(b, 0, n);
 84             }
 85             fis.close();
 86             bos.close();
 87             buffer = bos.toByteArray();
 88         } catch (IOException e) {
 89             e.printStackTrace();
 90         }
 91         return buffer;
 92     }
 93 
 94     /**
 95      * 4、int转byte
 96      *
 97      * @param i int数据
 98      * @return byte
 99      */
100     public static byte[] intToByteArray(int i) {
101         byte[] result = new byte[4];
102         result[0] = (byte) ((i >> 24) & 0xFF);
103         result[1] = (byte) ((i >> 16) & 0xFF);
104         result[2] = (byte) ((i >> 8) & 0xFF);
105         result[3] = (byte) (i & 0xFF);
106         return result;
107     }
108 
109 }
byte转换

 

上一篇:Java中二进制字节与十六进制互转


下一篇:C#调用Go版DLL