short、int、long与byte之间的转换工具类

 
  1. /** 
  2.  * 各基础类型与byte之间的转换 
  3.  * @author shanl 
  4.  * 
  5.  */  
  6. public class Utility {  
  7.       
  8.     /** 
  9.      * 将short转成byte[2] 
  10.      * @param a 
  11.      * @return 
  12.      */  
  13.     public static byte[] short2Byte(short a){  
  14.         byte[] b = new byte[2];  
  15.           
  16.         b[0] = (byte) (a >> 8);  
  17.         b[1] = (byte) (a);  
  18.           
  19.         return b;  
  20.     }  
  21.       
  22.     /** 
  23.      * 将short转成byte[2] 
  24.      * @param a 
  25.      * @param b 
  26.      * @param offset b中的偏移量 
  27.      */  
  28.     public static void short2Byte(short a, byte[] b, int offset){  
  29.         b[offset] = (byte) (a >> 8);  
  30.         b[offset+1] = (byte) (a);  
  31.     }  
  32.       
  33.     /** 
  34.      * 将byte[2]转换成short 
  35.      * @param b 
  36.      * @return 
  37.      */  
  38.     public static short byte2Short(byte[] b){  
  39.         return (short) (((b[0] & 0xff) << 8) | (b[1] & 0xff));  
  40.     }  
  41.       
  42.     /** 
  43.      * 将byte[2]转换成short 
  44.      * @param b 
  45.      * @param offset 
  46.      * @return  
  47.      */  
  48.     public static short byte2Short(byte[] b, int offset){  
  49.         return (short) (((b[offset] & 0xff) << 8) | (b[offset+1] & 0xff));  
  50.     }  
  51.   
  52.     /** 
  53.      * long转byte[8] 
  54.      *  
  55.      * @param a 
  56.      * @param b 
  57.      * @param offset 
  58.      *            b的偏移量 
  59.      */  
  60.     public static void long2Byte(long a, byte[] b, int offset) {          
  61.         b[offset + 0] = (byte) (a >> 56);  
  62.         b[offset + 1] = (byte) (a >> 48);  
  63.         b[offset + 2] = (byte) (a >> 40);  
  64.         b[offset + 3] = (byte) (a >> 32);  
  65.   
  66.         b[offset + 4] = (byte) (a >> 24);  
  67.         b[offset + 5] = (byte) (a >> 16);  
  68.         b[offset + 6] = (byte) (a >> 8);  
  69.         b[offset + 7] = (byte) (a);  
  70.     }  
  71.   
  72.     /** 
  73.      * byte[8]转long 
  74.      *  
  75.      * @param b 
  76.      * @param offset 
  77.      *            b的偏移量 
  78.      * @return 
  79.      */  
  80.     public static long byte2Long(byte[] b, int offset) {  
  81.          return ((((long) b[offset + 0] & 0xff) << 56)  
  82.          | (((long) b[offset + 1] & 0xff) << 48)  
  83.          | (((long) b[offset + 2] & 0xff) << 40)  
  84.          | (((long) b[offset + 3] & 0xff) << 32)  
  85.            
  86.          | (((long) b[offset + 4] & 0xff) << 24)  
  87.          | (((long) b[offset + 5] & 0xff) << 16)  
  88.          | (((long) b[offset + 6] & 0xff) << 8)  
  89.          | (((long) b[offset + 7] & 0xff) << 0));  
  90.     }  
  91.   
  92.     /** 
  93.      * byte[8]转long 
  94.      *  
  95.      * @param b 
  96.      * @return 
  97.      */  
  98.     public static long byte2Long(byte[] b) {  
  99.          return  
  100.          ((b[0]&0xff)<<56)|  
  101.          ((b[1]&0xff)<<48)|  
  102.          ((b[2]&0xff)<<40)|  
  103.          ((b[3]&0xff)<<32)|  
  104.           
  105.          ((b[4]&0xff)<<24)|  
  106.          ((b[5]&0xff)<<16)|  
  107.          ((b[6]&0xff)<<8)|  
  108.          (b[7]&0xff);  
  109.     }  
  110.   
  111.     /** 
  112.      * long转byte[8] 
  113.      *  
  114.      * @param a 
  115.      * @return 
  116.      */  
  117.     public static byte[] long2Byte(long a) {  
  118.         byte[] b = new byte[4 * 2];  
  119.   
  120.         b[0] = (byte) (a >> 56);  
  121.         b[1] = (byte) (a >> 48);  
  122.         b[2] = (byte) (a >> 40);  
  123.         b[3] = (byte) (a >> 32);  
  124.           
  125.         b[4] = (byte) (a >> 24);  
  126.         b[5] = (byte) (a >> 16);  
  127.         b[6] = (byte) (a >> 8);  
  128.         b[7] = (byte) (a >> 0);  
  129.   
  130.         return b;  
  131.     }  
  132.   
  133.     /** 
  134.      * byte数组转int 
  135.      *  
  136.      * @param b 
  137.      * @return 
  138.      */  
  139.     public static int byte2Int(byte[] b) {  
  140.         return ((b[0] & 0xff) << 24) | ((b[1] & 0xff) << 16)  
  141.                 | ((b[2] & 0xff) << 8) | (b[3] & 0xff);  
  142.     }  
  143.   
  144.     /** 
  145.      * byte数组转int 
  146.      *  
  147.      * @param b 
  148.      * @param offset 
  149.      * @return 
  150.      */  
  151.     public static int byte2Int(byte[] b, int offset) {  
  152.         return ((b[offset++] & 0xff) << 24) | ((b[offset++] & 0xff) << 16)  
  153.                 | ((b[offset++] & 0xff) << 8) | (b[offset++] & 0xff);  
  154.     }  
  155.   
  156.     /** 
  157.      * int转byte数组 
  158.      *  
  159.      * @param a 
  160.      * @return 
  161.      */  
  162.     public static byte[] int2Byte(int a) {  
  163.         byte[] b = new byte[4];  
  164.         b[0] = (byte) (a >> 24);  
  165.         b[1] = (byte) (a >> 16);  
  166.         b[2] = (byte) (a >> 8);  
  167.         b[3] = (byte) (a);  
  168.   
  169.         return b;  
  170.     }  
  171.   
  172.     /** 
  173.      * int转byte数组 
  174.      *  
  175.      * @param a 
  176.      * @param b 
  177.      * @param offset 
  178.      * @return 
  179.      */  
  180.     public static void int2Byte(int a, byte[] b, int offset) {        
  181.         b[offset++] = (byte) (a >> 24);  
  182.         b[offset++] = (byte) (a >> 16);  
  183.         b[offset++] = (byte) (a >> 8);  
  184.         b[offset++] = (byte) (a);  
  185.     }  
  186. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值