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

  1. //byte 与 int 的相互转换
  2. public static byte intToByte(int x) {
  3. return (byte) x;
  4. }
  5. public static int byteToInt(byte b) {
  6. //Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值
  7. return b & 0xFF;
  8. }

测试代码:

  1. //测试 int 转 byte
  2. int int0 = 234;
  3. byte byte0 = intToByte(int0);
  4. System.out.println("byte0=" + byte0);//byte0=-22
  5. //测试 byte 转 int
  6. int int1 = byteToInt(byte0);
  7. System.out.println("int1=" + int1);//int1=234

Java 中 byte 数组和 int 之间的转换源码:

  1. //byte 数组与 int 的相互转换
  2. public static int byteArrayToInt(byte[] b) {
  3. return   b[3] & 0xFF |
  4. (b[2] & 0xFF) << 8 |
  5. (b[1] & 0xFF) << 16 |
  6. (b[0] & 0xFF) << 24;
  7. }
  8. public static byte[] intToByteArray(int a) {
  9. return new byte[] {
  10. (byte) ((a >> 24) & 0xFF),
  11. (byte) ((a >> 16) & 0xFF),
  12. (byte) ((a >> 8) & 0xFF),
  13. (byte) (a & 0xFF)
  14. };
  15. }

测试代码:

  1. //测试 int 转 byte 数组
  2. int int2 = 1417;
  3. byte[] bytesInt = intToByteArray(int2);
  4. System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced
  5. //测试 byte 数组转 int
  6. int int3 = byteArrayToInt(bytesInt);
  7. System.out.println("int3=" + int3);//int3=1417

Java 中 byte 数组和 long 之间的转换源码:

  1. private static ByteBuffer buffer = ByteBuffer.allocate(8);
  2. //byte 数组与 long 的相互转换
  3. public static byte[] longToBytes(long x) {
  4. buffer.putLong(0, x);
  5. return buffer.array();
  6. }
  7. public static long bytesToLong(byte[] bytes) {
  8. buffer.put(bytes, 0, bytes.length);
  9. buffer.flip();//need flip
  10. return buffer.getLong();
  11. }

测试代码:

  1. //测试 long 转 byte 数组
  2. long long1 = 2223;
  3. byte[] bytesLong = longToBytes(long1);
  4. System.out.println("bytes=" + bytesLong);//bytes=[B@c17164
  5. //测试 byte 数组 转 long
  6. long long2 = bytesToLong(bytesLong);
  7. System.out.println("long2=" + long2);//long2=2223

整体工具类源码:

  1. import java.nio.ByteBuffer;
  2. public class Test {
  3. private static ByteBuffer buffer = ByteBuffer.allocate(8);
  4. public static void main(String[] args) {
  5. //测试 int 转 byte
  6. int int0 = 234;
  7. byte byte0 = intToByte(int0);
  8. System.out.println("byte0=" + byte0);//byte0=-22
  9. //测试 byte 转 int
  10. int int1 = byteToInt(byte0);
  11. System.out.println("int1=" + int1);//int1=234
  12. //测试 int 转 byte 数组
  13. int int2 = 1417;
  14. byte[] bytesInt = intToByteArray(int2);
  15. System.out.println("bytesInt=" + bytesInt);//bytesInt=[B@de6ced
  16. //测试 byte 数组转 int
  17. int int3 = byteArrayToInt(bytesInt);
  18. System.out.println("int3=" + int3);//int3=1417
  19. //测试 long 转 byte 数组
  20. long long1 = 2223;
  21. byte[] bytesLong = longToBytes(long1);
  22. System.out.println("bytes=" + bytesLong);//bytes=[B@c17164
  23. //测试 byte 数组 转 long
  24. long long2 = bytesToLong(bytesLong);
  25. System.out.println("long2=" + long2);//long2=2223
  26. }
  27. //byte 与 int 的相互转换
  28. public static byte intToByte(int x) {
  29. return (byte) x;
  30. }
  31. public static int byteToInt(byte b) {
  32. //Java 总是把 byte 当做有符处理;我们可以通过将其和 0xFF 进行二进制与得到它的无符值
  33. return b & 0xFF;
  34. }
  35. //byte 数组与 int 的相互转换
  36. public static int byteArrayToInt(byte[] b) {
  37. return   b[3] & 0xFF |
  38. (b[2] & 0xFF) << 8 |
  39. (b[1] & 0xFF) << 16 |
  40. (b[0] & 0xFF) << 24;
  41. }
  42. public static byte[] intToByteArray(int a) {
  43. return new byte[] {
  44. (byte) ((a >> 24) & 0xFF),
  45. (byte) ((a >> 16) & 0xFF),
  46. (byte) ((a >> 8) & 0xFF),
  47. (byte) (a & 0xFF)
  48. };
  49. }
  50. //byte 数组与 long 的相互转换
  51. public static byte[] longToBytes(long x) {
  52. buffer.putLong(0, x);
  53. return buffer.array();
  54. }
  55. public static long bytesToLong(byte[] bytes) {
  56. buffer.put(bytes, 0, bytes.length);
  57. buffer.flip();//need flip
  58. return buffer.getLong();
  59. }
  60. }

运行测试结果:
byte0=-22
int1=234
bytesInt=[B@de6ced
int3=1417
bytes=[B@c17164
long2=2223
参考文章1:http://stackoverflow.com/questions/7401550/how-to-convert-int-to-unsigned-byte-and-back
参考文章2:http://stackoverflow.com/questions/1936857/convert-integer-into-byte-array-java
参考文章3:http://stackoverflow.com/questions/4485128/how-do-i-convert-long-to-byte-and-back-in-java

04-25 12:35
查看更多