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. Redis配置文件介绍

    Redis在源码包中存放了一个Redis配置实例文件,文件中对各个配置点进行了简单的介绍,我也通过这个文件来对Redis的一些配置进行一些简单介绍. 一.UNITS(单位)[了解] 1.Redis服务 ...

  2. 一个完整的Installshield安装程序实例-转

    一个完整的Installshield安装程序实例—艾泽拉斯之海洋女神出品(一)---基本设置一 前言 Installshield可以说是最好的做安装程序的商业软件之一,不过因为功能的太过于强大,以至于 ...

  3. sublime text 3 使用简介

    2014年1月22日 09:47:50 2用了一段时间感觉不错,就是自带的高亮显示匹配标签或者代码块儿时有点儿不清楚,所以一直是sublime 开PHP,notepad++开html 现在想只用一个编 ...

  4. 升级Chrome后无法打开网页

    最近升级了网站,发现很多普通网站Chrome 都打不开了....  IE  可以正常打开,很是郁闷,重启电脑都不行. chrome://net-internals/#dns 点击如下按钮 清楚DNS缓 ...

  5. 推荐一些socket工具,TCP、UDP调试、抓包工具 (转载)

    还记得我在很久很久以前和大家推荐的Fiddler和Charles debugger么?他们都是HTTP的神器级调试工具,非常非常的好用.好工具能让你事半功倍,基本上,我是属于彻头彻尾的工具控. 假如有 ...

  6. poj1521

    霍夫曼编码,建树 #include <cstdio> #include <cstring> #include <queue> using namespace std ...

  7. Ibatis.Net 输出SQL语句学习(七)

    一.IBatis.net输出SQL语句 输出IBatis.net生成的SQL语句,能够方便调试. 在MapperHelper类中添加GetSql方法: /// <summary> /// ...

  8. ssh登录,Host key verification failed的几种处理方法

    - 修订历史History:  2011.05.22   初稿 - 系统: Ubuntu 10.04LTS  - 软件: SSH 使用SSH登录某台机器,有时因为server端的一些变动,会出现以下信 ...

  9. js如何判断访问来源是来自搜索引擎(蜘蛛人)还是直接访问

    以下javascript脚本代码可以实现判断访问是否来自搜索引擎.代码如下: ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 <scri ...

  10. 【LOJ】#2524. 「HAOI2018」反色游戏

    题解 如果一个联通块是一个树的话,方案数就一种,如果这个联通块还有别的边,那选了一条别的边就会把树上对应路径全部取反,所以方案数是\(2^{m - n + 1}\) 如果联通块数为\(c\)方案数为\ ...