public static byte[] toLH(int n) { byte[] b = new byte[4]; b[0] = (byte) (n >> 0 & 0xff); b[1] = (byte) (n >> 8 & 0xff); b[2] = (byte) (n >> 16 & 0xff); b[3] = (byte) (n >> 24 & 0xff); return b; } public static byte[] DoubleToBytes(double d){ //根据 IEEE 754 浮点“双精度格式”位布局,返回指定浮点值的表示形式,并保留 NaN 值。 Long value = Double.doubleToRawLongBits(d); byte[] b = new byte[8]; for(int i = 0 ; i<8;i++){ b[i] = (byte)((value>>8*i)&0xff); } return b; } public static String reverseCharArray(String s) { if (s == null) s = ""; char[] array = s.toCharArray(); String reverse = ""; for (int i = array.length - 1; i >= 0; i--) { reverse += array[i]; } return reverse; } public static byte[] byteMerger(byte[] data) { ByteArrayOutputStream os = new ByteArrayOutputStream(); try { os.write(data.length); os.write(data); } catch (IOException e) { e.printStackTrace(); } AppLog.e(DensityUtil.class, "byteMerger byte arrry length ---> " + os.toByteArray().length); return os.toByteArray(); } public static int convertByteArrToInt(byte[] byteArr, int index) { int intNum = 1; int intResult = 0; int ch1, ch2, ch3, ch4; //for(int k=index; j<intArr.length; j++, k+=4){ int k = index; ch1 = byteArr[k]; ch2 = byteArr[k + 1]; ch3 = byteArr[k + 2]; ch4 = byteArr[k + 3]; if (ch1 < 0) { ch1 = 256 + ch1; } if (ch2 < 0) { ch2 = 256 + ch2; } if (ch3 < 0) { ch3 = 256 + ch3; } if (ch4 < 0) { ch4 = 256 + ch4; } intResult = (ch1) + (ch2 << 8) + (ch3 << 16) + (ch4 << 24); return intResult; } public static byte[] convertStrToByteArr(String str) { byte[] bs = str.getBytes(); return bs; } public static String convertByteArrToString(byte[] byteArr, int index, int len) { int newlen = 0; for (int i = index; i < index + len; i++) { if (byteArr[i] == 0) break; newlen += 1; } if (newlen == 0) return null; String str = new String(byteArr, index, newlen); return str; }
Android 大端模式转换
最新推荐文章于 2023-03-17 10:32:43 发布