Socket编程int与byte array之间互相转换

来源:互联网 发布:淘宝怎么修改评价 编辑:程序博客网 时间:2024/06/06 00:52

public static byte[] intToByteArray(int i) {
byte[] result = new byte[4];
//由高位到低位
result[0] = (byte)((i >> 24) & 0xFF);
result[1] = (byte)((i >> 16) & 0xFF);
result[2] = (byte)((i >> 8) & 0xFF);
result[3] = (byte)(i & 0xFF);
return result;
}
/**
* byte[]转int
* @param bytes
* @return
*/
public static int byteArrayToInt(byte[] bytes) {
int value= 0;
//由高位到低位
for (int i = 0; i < 4; i++) {
int shift= (4 - 1 - i) * 8;
value +=(bytes[i] & 0x000000FF) << shift;//往高位游
}
return value;
}

整体工具类代码

  • import java.nio.ByteBuffer;  
  •   
  •   
  • public class Test {  
  •       
  •     private static ByteBuffer buffer = ByteBuffer.allocate(8);      
  •   
  •     public static void main(String[] args) {  
  •           
  •         //测试 int 转 byte  
  •         int int0 = 234;  
  •         byte byte0 = intToByte(int0);  
  •         System.out.println(”byte0=” + byte0);//byte0=-22  
  •         //测试 byte 转 int  
  •         int int1 = byteToInt(byte0);  
  •         System.out.println(”int1=” + int1);//int1=234  
  •           
  •           
  •           
  •         //测试 int 转 byte 数组  
  •         int int2 = 1417;  
  •         byte[] bytesInt = intToByteArray(int2);  
  •         System.out.println(”bytesInt=” + bytesInt);//bytesInt=[B@de6ced  
  •         //测试 byte 数组转 int  
  •         int int3 = byteArrayToInt(bytesInt);  
  •         System.out.println(”int3=” + int3);//int3=1417  
  •           
  •           
  •         //测试 long 转 byte 数组  
  •         long long1 = 2223;  
  •         byte[] bytesLong = longToBytes(long1);  
  •         System.out.println(”bytes=” + bytesLong);//bytes=[B@c17164  
  •         //测试 byte 数组 转 long  
  •         long long2 = bytesToLong(bytesLong);  
  •         System.out.println(”long2=” + long2);//long2=2223  
  •     }  
  •       
  •       
  •     //byte 与 int 的相互转换  
  •     public static byte intToByte(int x) {  
  •         return (byte) x;  
  •     }  
  •       
  •     public static int byteToInt(byte b) {  
  •         //Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值  
  •         return b & 0xFF;  
  •     }  
  •       
  •     //byte 数组与 int 的相互转换  
  •     public static int byteArrayToInt(byte[] b) {  
  •         return   b[3] & 0xFF |  
  •                 (b[2] & 0xFF) << 8 |  
  •                 (b[1] & 0xFF) << 16 |  
  •                 (b[0] & 0xFF) << 24;  
  •     }  
  •   
  •     public static byte[] intToByteArray(int a) {  
  •         return new byte[] {  
  •             (byte) ((a >> 24) & 0xFF),  
  •             (byte) ((a >> 16) & 0xFF),     
  •             (byte) ((a >> 8) & 0xFF),     
  •             (byte) (a & 0xFF)  
  •         };  
  •     }  
  •   
  •     //byte 数组与 long 的相互转换  
  •     public static byte[] longToBytes(long x) {  
  •         buffer.putLong(0, x);  
  •         return buffer.array();  
  •     }  
  •   
  •     public static long bytesToLong(byte[] bytes) {  
  •         buffer.put(bytes, 0, bytes.length);  
  •         buffer.flip();//need flip   
  •         return buffer.getLong();  
  •     }  
  •   
  • }  
  • 0 0
    原创粉丝点击