Java 中 byte、byte 数组和 int、long 之间的转换

来源:互联网 发布:网络公司源码 编辑:程序博客网 时间:2024/06/06 02:21

本文根据原文整理而成:http://blog.csdn.net/defonds/article/details/8782785

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

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


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

[java] view plain copy print?
  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.   
  9. public static byte[] intToByteArray(int a) {  
  10.     return new byte[] {  
  11.         (byte) ((a >> 24) & 0xFF),  
  12.         (byte) ((a >> 16) & 0xFF),     
  13.         (byte) ((a >> 8) & 0xFF),     
  14.         (byte) (a & 0xFF)  
  15.     };  
  16. }  



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

[java] view plain copy print?
  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.   
  8.    public static long bytesToLong(byte[] bytes) {  
  9.        buffer.put(bytes, 0, bytes.length);  
  10.        buffer.flip();//need flip   
  11.        return buffer.getLong();  
  12.    }  



        整体工具类源码:

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

        运行测试结果:
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
0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 一岁宝宝多动怎么办 3岁前儿童多动症怎么办 好动症的孩子该怎么办 18个月宝宝腹泻怎么办 8个月小孩发烧怎么办 八个月大宝宝发烧怎么办 8个月宝宝拉稀水怎么办 八个月宝宝38度怎么办 8个月的宝宝发烧怎么办 八个月大的宝宝拉肚子怎么办 9孩子上课坐不住怎么办 3岁宝宝太皮怎么办 4岁的宝宝多动症怎么办 小孩好动注意力不集中怎么办 乐扣加热变形了怎么办 3岁宝宝咳嗽厉害怎么办 六个月婴儿呕奶怎么办 刚出生婴儿呕奶怎么办 宝宝拉颗粒便便怎么办 奶水不够宝宝又不吃奶粉怎么办 一岁突然不吃饭怎么办 母乳不够吃宝宝不吃奶粉怎么办 八个月不吃辅食怎么办 孩子长得太快怎么办 反复发烧到39度怎么办 儿童发烧到39度怎么办 7岁儿童发烧40度怎么办 7岁反复发烧39度怎么办 宝宝烧到39.5度怎么办 3岁儿童发烧39度怎么办 孩子发高烧怎么办39度5 3岁宝宝不吃水果怎么办 2岁宝宝不吃水果怎么办 4岁宝宝不吃水果怎么办 过早竖抱婴儿了怎么办 3个月宝宝认生怎么办 10天婴儿不拉屎怎么办 3个月宝宝不吃奶粉怎么办 婴儿吃青菜吃多怎么办 2月宝宝消化不好怎么办 吃母乳的宝宝便秘怎么办