package cn.com.heaton.blelibrary.ble.utils; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; /** * 字节的转换 * Created by jerry on 2017/4/27. */ public class ByteUtils { //inputstream转byte[] public static byte[] toByteArray(InputStream input) { // ByteARRAYOUTPUTSTREAM output = new ByteArrayOutputStream(); ByteArrayOutputStream output = new ByteArrayOutputStream(); byte[] buffer = new byte[4096]; int n = 0; try { while (-1 != (n = input.read(buffer))) { output.write(buffer, 0, n); } } catch (IOException e) { e.printStackTrace(); } return output.toByteArray(); } //将字节数组转换为short类型,即统计字符串长度 public static short bytes2Short2(byte[] b) { short i = (short) (((b[1] & 0xff) << 8) | b[0] & 0xff); return i; } //将字节数组转换为16进制字符串 public static String BinaryToHexString(byte[] bytes) { String hexStr = "0123456789ABCDEF"; String result = ""; String hex = ""; for (byte b : bytes) { hex = String.valueOf(hexStr.charAt((b & 0xF0) >> 4)); hex += String.valueOf(hexStr.charAt(b & 0x0F)); result += hex + " "; } return result; } //3.short转换为byte数组 public static byte[] short2Bytes(short value) { byte[] data = new byte[2]; data[0] = (byte) (value >> 8 & 0xff); data[1] = (byte) (value & 0xFF); return data; } /** * 将int转化成byte[] * * @param res 要转化的整数 * @return 对应的byte[] */ public static byte[] int2byte(int res) { byte[] targets = new byte[4]; targets[0] = (byte) (res & 0xff);// 最低位 targets[1] = (byte) ((res >> 8) & 0xff);// 次低位 targets[2] = (byte) ((res >> 16) & 0xff);// 次高位 targets[3] = (byte) (res >>> 24);// 最高位,无符号右移。 return targets; } // int zhuanhuawei byte[4] /** * 将byte[]转化成int // byte[] int mingitan byte[] int * @param res 要转化的byte[] * @return 对应的整数 */ public static int byte2int(byte[] res) { int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00) | ((res[2] << 24) >>> 8) | (res[3] << 24); return targets; } public static String byteArrayToHexStr(byte[] byteArray) { if (byteArray == null){ return null; } char[] hexArray = "0123456789ABCDEF".toCharArray(); char[] hexChars = new char[byteArray.length * 2]; for (int j = 0; j < byteArray.length; j++) { int v = byteArray[j] & 0xFF; hexChars[j * 2] = hexArray[v >>> 4]; hexChars[j * 2 + 1] = hexArray[v & 0x0F]; } return new String(hexChars); } public static byte[] hexStrToByteArray(String str) { if (str == null) { return null; } if (str.length() == 0) { return new byte[0]; } byte[] byteArray = new byte[str.length() / 2]; for (int i = 0; i < byteArray.length; i++){ String subStr = str.substring(2 * i, 2 * i + 2); byteArray[i] = ((byte) Integer.parseInt(subStr, 16)); } return byteArray; } }
Android 字节转化
最新推荐文章于 2023-03-02 22:19:29 发布