zoukankan      html  css  js  c++  java
  • Java 中 byte、byte 数组和 int、long 之间的转换

            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] view plain copy
     
     print?
    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 之间的转换源码:

    [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] view plain copy
     
     print?
    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 之间的转换源码:

    [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. //测试 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  

            整体工具类源码:

    [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

  • 相关阅读:
    二层设备与三层设备的区别--总结
    转载-vim配置收藏
    Docker入门
    Docker入门
    Docker入门
    Docker入门
    Docker入门
    树莓派进阶之路 (037)
    基于Centos搭建个人 Leanote 云笔记本
    基于CentOS搭建私有云服务
  • 原文地址:https://www.cnblogs.com/goody9807/p/6437854.html
Copyright © 2011-2022 走看看