`
klts
  • 浏览: 39257 次
  • 性别: Icon_minigender_2
  • 来自: 成都
社区版块
存档分类
最新评论
  • klts: 我一直用的jdk1.6.0_13,感觉还可以
    JDK
  • wuyulunbi: 现在jdk什么版本的稳定
    JDK

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; 
  } 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics