byte陣列與int ,float,string型別之間的互相轉換
阿新 • • 發佈:2018-12-19
/*這些都是我在用modbusTcp協議利用socket與plc互動資料時處理資料的方法,傳輸報文資料*/ //int轉兩個位元組byte陣列高位到低位 public static byte[] IntToLBytes(int paramInt) { byte[] res = new byte[2]; for(int i = 0; i < 2; i++) res[2 - 1 - i] = (byte)(paramInt >> (8 * i)); return res; }
//int 轉一個byte位元組十六進位制 public static byte[] formatRegister1(int registerValue) { byte[] res=new byte[1]; res[0]=(byte)(registerValue & 0xFF); return res; } //int 轉一個byte位元組十六進位制四個位元組低位在前 public static byte[] formatRegister2(int registerValue) { byte[] b = new byte[4]; b[1]=(byte)((registerValue >> 8 * 0) & 0xff); b[0]=(byte)((registerValue >> 8 * 1) & 0xff); b[3]=(byte)((registerValue >> 8 * 2) & 0xff); b[2]=(byte)((registerValue >> 8 * 3) & 0xff); return b; }
/** * byte陣列轉int型別的物件 * @param bytes * @return 轉換高位在前低位在後的byte */ public static int ByteTOIntHighLow(byte[]bytes) { return (bytes[0]&0xff)<<24 | (bytes[1]&0xff)<<16 | (bytes[2]&0xff)<<8 | (bytes[3]&0xff); } /** * byte陣列轉int型別的物件 * @param bytes * @return 轉換低位在前高位在後的byte */ public static int Byte2IntLowHigh(byte[]bytes) { return (bytes[2]&0xff)<<24 | (bytes[3]&0xff)<<16 | (bytes[0]&0xff)<<8 | (bytes[1]&0xff); }
//處理byte陣列轉int陣列 public static int[] byteToIntArray(byte[] arr){ byte[] te=new byte[2]; int count=-1; int[] intArays = new int[arr.length/2]; for(int i=0;i<arr.length/2;i++){ count++; System.arraycopy(arr, count*2, te, 0, 2); int b= dataConversion.Byte2IntLowHigh2byte(te); intArays[i]=b; } return intArays; }
* result 傳過來的結果 * number * */ public static String byteToBinary(byte[] byteArr) { int b = byteArr[8]; byte[] buff = new byte[b]; System.arraycopy(byteArr, 9, buff, 0, byteArr.length - 9); String a = ""; for (int i = 0; i < buff.length; i++) { String tString = Integer.toBinaryString((buff[i] & 0xFF) + 0x100).substring(1); StringBuffer stringBuffer = new StringBuffer(tString); //把字串調換順序 stringBuffer.reverse(); a += stringBuffer; } System.out.println("tString:" + a); return a; }
//string轉int陣列
public static int[] stringToIntArrays(String string) { // String s = "010102010101010101010"; // BigInteger b = new BigInteger(s); // // System.out.println("結果是:/n" + b); int[] intArrays=new int[string.length()]; for(int i=0;i<string.length();i++){ //先由字串轉換成char,再轉換成String,然後Integer intArrays[i] = Integer.parseInt( (String.valueOf(string.charAt(i))).trim()); } System.out.println("結果是:"+Arrays.toString(intArrays)); return intArrays; }
//byte陣列轉string public static String toHexString(byte[] byteArray) { if (byteArray == null || byteArray.length < 1) throw new IllegalArgumentException("this byteArray must not be null or empty"); final StringBuilder hexString = new StringBuilder(); for (int i = 0; i < byteArray.length; i++) { if ((byteArray[i] & 0xff) < 0x10)//0~F前面不零 hexString.append("0"); hexString.append(Integer.toHexString(0xFF & byteArray[i])); } return hexString.toString().toLowerCase(); }
//四個位元組的byte陣列轉float
public static float byteToFloat(byte[] byteArray){
int res=dataConversion.Byte2IntLowHigh(buff); float m= Float.intBitsToFloat(res);
return m;
}