首页 / JAVA / Java-byte转换
Java-byte转换
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java-byte转换,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3478字,纯文字阅读大概需要5分钟。
内容图文
![Java-byte转换](/upload/InfoBanner/zyjiaocheng/628/9bba3d043e024962806cc3c0e2e21d9d.jpg)
Java-byte转换
![Java-byte转换 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501064809611.jpg)
![Java-byte转换 - 文章图片](/upload/getfiles/0001/2021/5/1/20210501064809637.jpg)
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-byte转换全部内容,希望文章能够帮你解决Java-byte转换所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。