java byte數組與int,long,short,byte轉換


[java]  view plain copy
 
  1. public class DataTypeChangeHelper {  
  2.     /** 
  3.      * 將一個單字節的byte轉換成32位的int 
  4.      *  
  5.      * @param b 
  6.      *            byte 
  7.      * @return convert result 
  8.      */  
  9.     public static int unsignedByteToInt(byte b) {  
  10.         return (int) b & 0xFF;  
  11.     }  
  12.   
  13.     /** 
  14.      * 將一個單字節的Byte轉換成十六進制的數 
  15.      *  
  16.      * @param b 
  17.      *            byte 
  18.      * @return convert result 
  19.      */  
  20.     public static String byteToHex(byte b) {  
  21.         int i = b & 0xFF;  
  22.         return Integer.toHexString(i);  
  23.     }  
  24.   
  25.     /** 
  26.      * 將一個4byte的數組轉換成32位的int 
  27.      *  
  28.      * @param buf 
  29.      *            bytes buffer 
  30.      * @param byte[]中開始轉換的位置 
  31.      * @return convert result 
  32.      */  
  33.     public static long unsigned4BytesToInt(byte[] buf, int pos) {  
  34.         int firstByte = 0;  
  35.         int secondByte = 0;  
  36.         int thirdByte = 0;  
  37.         int fourthByte = 0;  
  38.         int index = pos;  
  39.         firstByte = (0x000000FF & ((int) buf[index]));  
  40.         secondByte = (0x000000FF & ((int) buf[index + 1]));  
  41.         thirdByte = (0x000000FF & ((int) buf[index + 2]));  
  42.         fourthByte = (0x000000FF & ((int) buf[index + 3]));  
  43.         index = index + 4;  
  44.         return ((long) (firstByte << 24 | secondByte << 16 | thirdByte << 8 | fourthByte)) & 0xFFFFFFFFL;  
  45.     }  
  46.   
  47.     /** 
  48.      * 將16位的short轉換成byte數組 
  49.      *  
  50.      * @param s 
  51.      *            short 
  52.      * @return byte[] 長度為2 
  53.      * */  
  54.     public static byte[] shortToByteArray(short s) {  
  55.         byte[] targets = new byte[2];  
  56.         for (int i = 0; i < 2; i++) {  
  57.             int offset = (targets.length - 1 - i) * 8;  
  58.             targets[i] = (byte) ((s >>> offset) & 0xff);  
  59.         }  
  60.         return targets;  
  61.     }  
  62.   
  63.     /** 
  64.      * 將32位整數轉換成長度為4的byte數組 
  65.      *  
  66.      * @param s 
  67.      *            int 
  68.      * @return byte[] 
  69.      * */  
  70.     public static byte[] intToByteArray(int s) {  
  71.         byte[] targets = new byte[2];  
  72.         for (int i = 0; i < 4; i++) {  
  73.             int offset = (targets.length - 1 - i) * 8;  
  74.             targets[i] = (byte) ((s >>> offset) & 0xff);  
  75.         }  
  76.         return targets;  
  77.     }  
  78.   
  79.     /** 
  80.      * long to byte[] 
  81.      *  
  82.      * @param s 
  83.      *            long 
  84.      * @return byte[] 
  85.      * */  
  86.     public static byte[] longToByteArray(long s) {  
  87.         byte[] targets = new byte[2];  
  88.         for (int i = 0; i < 8; i++) {  
  89.             int offset = (targets.length - 1 - i) * 8;  
  90.             targets[i] = (byte) ((s >>> offset) & 0xff);  
  91.         }  
  92.         return targets;  
  93.     }  
  94.   
  95.     /**32位int轉byte[]*/  
  96.     public static byte[] int2byte(int res) {  
  97.         byte[] targets = new byte[4];  
  98.         targets[0] = (byte) (res & 0xff);// 最低位  
  99.         targets[1] = (byte) ((res >> 8) & 0xff);// 次低位  
  100.         targets[2] = (byte) ((res >> 16) & 0xff);// 次高位  
  101.         targets[3] = (byte) (res >>> 24);// 最高位,無符號右移。  
  102.         return targets;  
  103.     }  
  104.   
  105.     /** 
  106.      * 將長度為2的byte數組轉換為16位int 
  107.      *  
  108.      * @param res 
  109.      *            byte[] 
  110.      * @return int 
  111.      * */  
  112.     public static int byte2int(byte[] res) {  
  113.         // res = InversionByte(res);  
  114.         // 一個byte數據左移24位變成0x??000000,再右移8位變成0x00??0000  
  115.         int targets = (res[0] & 0xff) | ((res[1] << 8) & 0xff00); // | 表示安位或  
  116.         return targets;  
  117.     }  
  118. }  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM