int和byte[]互转

来源:互联网 发布:ps淘宝主图制作教程 编辑:程序博客网 时间:2024/06/11 03:34

原地址 http://blog.csdn.net/zgcqflqinhao/article/details/53256862


[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * int转byte[] 
  3.  */  
  4. public static byte[] intToBytes(int i) {  
  5.     byte[] bytes = new byte[4];  
  6.     bytes[0] = (byte) (i & 0xff);  
  7.     bytes[1] = (byte) ((i >> 8) & 0xff);  
  8.     bytes[2] = (byte) ((i >> 16) & 0xff);  
  9.     bytes[3] = (byte) ((i >> 24) & 0xff);  
  10.     return bytes;  
  11. }  

接收的时候再转一下即可

[java] view plain copy
 在CODE上查看代码片派生到我的代码片
  1. /** 
  2.  * byte[]转int 
  3.  */  
  4. public static int bytesToInt(byte[] bytes) {  
  5.     int i;  
  6.     i = (int) ((bytes[0] & 0xff) | ((bytes[1] & 0xff) << 8)  
  7.             | ((bytes[2] & 0xff) << 16) | ((bytes[3] & 0xff) << 24));  
  8.     return i;  
  9. }  

0 0
原创粉丝点击