资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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

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

10年积累的网站建设、成都网站设计经验,可以快速应对客户对网站的新想法和需求。提供各种问题对应的解决方案。让选择我们的客户得到更好、更有力的网络服务。我虽然不认识你,你也不认识我。但先制作网站后付款的网站建设流程,更有南靖免费网站建设让你可以放心的选择与我们合作。

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

总结

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


网页题目:Java中byte、byte数组与int、long的转换详解
网页地址:http://cdkjz.cn/article/jdgghd.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

业务热线:400-028-6601 / 大客户专线   成都:13518219792   座机:028-86922220