ICode9

精准搜索请尝试: 精确搜索
首页 > 编程语言> 文章详细

Java-byte转换

2020-07-16 17:36:20  阅读:235  来源: 互联网

标签:转换 16 hexString length result 0xFF 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转换

 

标签:转换,16,hexString,length,result,0xFF,Java,byte
来源: https://www.cnblogs.com/StefanieYang/p/13323873.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有