| | |
| | | } |
| | | |
| | | |
| | | public static float byteToFloat(byte b1, byte b2, byte b3, byte b4){ |
| | | byte[] mByte = new byte[4]; |
| | | mByte[0] = b1; |
| | | mByte[1] = b2; |
| | | mByte[2] = b3; |
| | | mByte[3] = b4; |
| | | return byteArrayToFloat(mByte); |
| | | } |
| | | |
| | | // 从byte数组的index处的连续4个字节获得一个int |
| | | public static int getInt(byte[] arr) { |
| | | return (0xff000000 & (arr[0] << 24)) | |
| | | (0x00ff0000 & (arr[1] << 16)) | |
| | | (0x0000ff00 & (arr[2] << 8)) | |
| | | (0x000000ff & arr[3]); |
| | | } |
| | | |
| | | // float转换为byte[4]数组 |
| | | public static byte[] getByteArray(float f) { |
| | | int intbits = Float.floatToIntBits(f);//将float里面的二进制串解释为int整数 |
| | | return getByteArray(intbits); |
| | | } |
| | | |
| | | // 从byte数组的index处的连续4个字节获得一个float |
| | | public static float byteArrayToFloat(byte[] arr) { |
| | | try { |
| | | return Float.intBitsToFloat(getInt(arr)); |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | return 0; |
| | | } |
| | | } |
| | | |
| | | // private byte[] formatStyleHDLResponse(String responsePart1, String responsePart2, int maxLen) throws UnsupportedEncodingException { |
| | | // String responsePart3 = HDL_END_STR; |
| | |
| | | // return; |
| | | // } |
| | | |
| | | //------------------------------------------------------- |
| | | //字节数组转转hex字符串,可选长度 |
| | | public static String ByteArrToHex(byte[] inBytArr, int offset, int byteCount) { |
| | | StringBuilder strBuilder = new StringBuilder(); |
| | | int j = byteCount; |
| | | for (int i = offset; i < j; i++) { |
| | | strBuilder.append(Byte2Hex(Byte.valueOf(inBytArr[i]))); |
| | | } |
| | | return strBuilder.toString(); |
| | | } |
| | | |
| | | //------------------------------------------------------- |
| | | //1字节转2个Hex字符 |
| | | public static String Byte2Hex(Byte inByte) { |
| | | return String.format("%02x", new Object[]{inByte}).toUpperCase(); |
| | | } |
| | | } |