当前位置: 移动技术网 > IT编程>开发语言>Java > Java中byte、byte数组与int、long的转换详解

Java中byte、byte数组与int、long的转换详解

2019年07月22日  | 移动技术网IT编程  | 我要评论

一、java 中 byte 和 int 之间的转换源码:

//byte 与 int 的相互转换 
public static byte inttobyte(int x) { 
 return (byte) x; 
} 
 
public static int bytetoint(byte b) { 
 //java 总是把 byte 当做有符处理;我们可以通过将其和 0xff 进行二进制与得到它的无符值 
 return b & 0xff; 
} 

测试代码:

//测试 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 

二、java 中 byte 数组和 int 之间的转换源码:

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

测试代码:

//测试 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 

三、java 中 byte 数组和 long 之间的转换源码:

private static bytebuffer buffer = bytebuffer.allocate(8); 
//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(); 
 } 

测试代码:

//测试 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 

四、整体工具类源码:

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

运行测试结果:

byte0=-22
int1=234
bytesint=[b@de6ced
int3=1417
bytes=[b@c17164
long2=2223

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网