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

Java 中 byte、byte 数组和 int、long 之间的转换的更多相关文章

  1. C++string,char* 字符数组,int类型之间的转换

    string.int 常见类型之间相互转换 int & string 之间的转换 C++中更多的是使用流对象来实现类型转换 针对流对象 sstream实现 int,float 类型都可以实现 ...

  2. Java中的基本数据类型和基本数据类型之间的转换

    在Java中有8中基本数据类型,分别为: 整型: byte.short.int.long 浮点型:float.double 布尔型:boolean 字符型:char. byte:    8位,  封装 ...

  3. java中的字符,字符串,数字之间的转换

    string 和int之间的转换 string转换成int  :Integer.valueOf("12") int转换成string : String.valueOf(12) ch ...

  4. java中的字符、字符串及数字之间的转换(转)

    一.string 和int之间的转换 1.string转换成int  :Integer.valueOf("12") 2.int转换成string : String.valueOf( ...

  5. 详解java中的byte类型

    Java也提供了一个byte数据类型,并且是基本类型.java byte是做为最小的数字来处理的,因此它的值域被定义为-128~127,也就是signed byte.下面这篇文章主要给大家介绍了关于j ...

  6. java中的byte

    8 bit (位) = 1 Byte (字节) java中的byte就是Byte 1024 Byte = 1KB 1024 KB = 1MB 1:“字节”是byte,“位”是bit : 2: 1 by ...

  7. 深入理解java中的byte类型

    作者 | 进击的石头--GO! 来源 | https://www.cnblogs.com/zl181015/p/9435035.html#4432849 Java也提供了一个byte数据类型,并且是基 ...

  8. 谈谈Java中整数类型(short int long)的存储方式

    在java中的整数类型有四种,分别是byte short in long,本文重点给大家介绍java中的整数类型(short int long),由于byte只是一个字节0或1,在此就不多说了,对ja ...

  9. java中整数的默认为int类型的一些问题

    thingking in java 读书感悟 作者 :淮左白衣 写于2018年4月8日17:51:44 关于整数的默认类型,以及会产生的一些小问题 涉及基本数据类型的重载 关于整数的默认类型,以及会产 ...

  10. Java中创建泛型数组

    Java中创建泛型数组 使用泛型时,我想很多人肯定尝试过如下的代码,去创建一个泛型数组 T[] array = new T[]; 当我们写出这样的代码时编译器会报Cannot create a gen ...

随机推荐

  1. CSS border系列

    本文更新版链接 一.border 关于border的3个属性,分别为border-width.border-style.border-color. 其中,border-color默认为元素内容的前景色 ...

  2. 安装informatic过程中的错误

    1.Check if the DISPLAY variable is set export DISPLAY=192.168.3.201:0.0 在注销用户并切换到oracle或者infa 用户,就可以 ...

  3. Centos6.5下rsync+inotify的配置详解

    Centos 6.5配置rsync+inotify实现文件实时同步 1.安装rsync(两台机器执行相同的步骤) yum install gcc yum install rsyncd xinetd - ...

  4. aliyun EC2配置利用filezilla配置ftp服务

    项目需要在阿里云EC2虚拟主机上配置ftp服务器,看了阿里云的教程可以使用filezilla配置,但一直遇到了一些问题.现记录一些步骤,避免以后出现类似问题. 1安装filezilla server ...

  5. linux中断申请之request_threaded_irq【转】

    转自:http://blog.chinaunix.net/xmlrpc.php?r=blog/article&uid=21977330&id=3755609 在linux里,中断处理分 ...

  6. asp.net动态增加服务器端控件并提交表单

    为什么要用原生的呢? 1.目的 原生出现浏览器兼容性问题 极少,不用测试多浏览兼容性 .需要考虑到市面上的其他垃圾浏览器. 2.性能不好 如果不考虑第一条 你可以换一种方式 直接上代码 .aspx页面 ...

  7. ida自动编译配置

    这个勾选上,就会出现

  8. 数组用console.log输出

    输出的时候,如果前面有字符串,那么输出的就是整个字符串

  9. opencv 彩色图像分割(inrange)

    灰度图像大多通过算子寻找边缘和区域生长融合来分割图像. 彩色图像增加了色彩信息,可以通过不同的色彩值来分割图像,常用彩色空间HSV/HSI, RGB, LAB等都可以用于分割! 笔者主要介绍inran ...

  10. 图学ES6-3.变量的解构赋值