java中byte数组与int ,float,string类型之间的互相转换

/*这些都是我在用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;

}

没有更多推荐了,返回首页