读书人

byte[]跟int间的转换

发布时间: 2012-08-31 12:55:03 作者: rapoo

byte[]和int间的转换

  public static byte[] i2b(int i) {    byte[] bt = new byte[4];    bt[3] = (byte) (0xff & i);    bt[2] = (byte) ((0xff00 & i) >> 8);    bt[1] = (byte) ((0xff0000 & i) >> 16);    bt[0] = (byte) ((0xff000000 & i) >> 24);    return bt;  }    private static int toInt(byte b){     if(b >= 0) return (int)b;     else return (int) (b + 256);   }   private static int b2i(byte[] byteValue){     if(byteValue.length != 4)       return 0;     int intValue = 0;     try{       intValue = toInt(byteValue[0]);       intValue = (intValue << 8) + toInt(byteValue[1]);       intValue = (intValue << 8) + toInt(byteValue[2]);       intValue = (intValue << 8) + toInt(byteValue[3]);     }     catch(Exception e){       e.printStackTrace();     }     return intValue;   } 

?

读书人网 >编程

热点推荐