java 关于int 和byte间的转换,请教各位!

int to byte[]  

1/  new   String(new   Integer(int   value)).getBytes()  

2/public   synchronized   static   byte[]   Int2Byte(int   send)   throws   IOException  
    {  
    byte   []   bTemp   =   new   byte[4];  
    //System.out.println("send:   "+send+   "     Hex:"   +   Integer.toHexString(send));  
    for   (int   i=0;i<4;i++)  
    {  
    bTemp[i]   =   (byte)((send   >>(i*8))&   0xff);  
    //System.out.println("send   byte"   +((send   >>(i*16))&0xff));    
    }  

//   取正  
          private   int   toInt(byte   b){  
                  if(b   >=   0)   return   (int)b;  
                  else               return   (int)(b   +   256);  
          }  
   
          //   4   byte   Array   to   int  
          private   int   byteArray4ToInt(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;  
          }  
   
  -----   这是我项目里写的工具函数,要注意的是byte-->int的时候,如果是负数  
  -----   要+256,代码如上 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值