java中byte数组与int,long,short间的转换

来源:互联网 发布:东北软件学院地址 编辑:程序博客网 时间:2024/05/02 02:05
  1. package com.util;  
  2.   
  3. /** 
  4.  *  
  5.  * <ul> 
  6.  * <li>文件名称: com.born.util.ByteUtil.java</li> 
  7.  * <li>文件描述: byte转换工具</li> 
  8.  * <li>版权所有: 版权所有(C)2001-2006</li> 
  9.  * <li>公 司: bran</li> 
  10.  * <li>内容摘要:</li> 
  11.  * <li>其他说明:</li> 
  12.  * <li>完成日期:2011-7-18</li> 
  13.  * <li>修改记录0:无</li> 
  14.  * </ul> 
  15.  *  
  16.  * @version 1.0 
  17.  * @author 许力多 
  18.  */  
  19. public class ByteUtil {  
  20.     /** 
  21.      * 转换short为byte 
  22.      *  
  23.      * @param b 
  24.      * @param s 
  25.      *            需要转换的short 
  26.      * @param index 
  27.      */  
  28.     public static void putShort(byte b[], short s, int index) {  
  29.         b[index + 1] = (byte) (s >> 8);  
  30.         b[index + 0] = (byte) (s >> 0);  
  31.     }  
  32.   
  33.     /** 
  34.      * 通过byte数组取到short 
  35.      *  
  36.      * @param b 
  37.      * @param index 
  38.      *            第几位开始取 
  39.      * @return 
  40.      */  
  41.     public static short getShort(byte[] b, int index) {  
  42.         return (short) (((b[index + 1] << 8) | b[index + 0] & 0xff));  
  43.     }  
  44.   
  45.     /** 
  46.      * 转换int为byte数组 
  47.      *  
  48.      * @param bb 
  49.      * @param x 
  50.      * @param index 
  51.      */  
  52.     public static void putInt(byte[] bb, int x, int index) {  
  53.         bb[index + 3] = (byte) (x >> 24);  
  54.         bb[index + 2] = (byte) (x >> 16);  
  55.         bb[index + 1] = (byte) (x >> 8);  
  56.         bb[index + 0] = (byte) (x >> 0);  
  57.     }  
  58.   
  59.     /** 
  60.      * 通过byte数组取到int 
  61.      *  
  62.      * @param bb 
  63.      * @param index 
  64.      *            第几位开始 
  65.      * @return 
  66.      */  
  67.     public static int getInt(byte[] bb, int index) {  
  68.         return (int) ((((bb[index + 3] & 0xff) << 24)  
  69.                 | ((bb[index + 2] & 0xff) << 16)  
  70.                 | ((bb[index + 1] & 0xff) << 8) | ((bb[index + 0] & 0xff) << 0)));  
  71.     }  
  72.   
  73.     /** 
  74.      * 转换long型为byte数组 
  75.      *  
  76.      * @param bb 
  77.      * @param x 
  78.      * @param index 
  79.      */  
  80.     public static void putLong(byte[] bb, long x, int index) {  
  81.         bb[index + 7] = (byte) (x >> 56);  
  82.         bb[index + 6] = (byte) (x >> 48);  
  83.         bb[index + 5] = (byte) (x >> 40);  
  84.         bb[index + 4] = (byte) (x >> 32);  
  85.         bb[index + 3] = (byte) (x >> 24);  
  86.         bb[index + 2] = (byte) (x >> 16);  
  87.         bb[index + 1] = (byte) (x >> 8);  
  88.         bb[index + 0] = (byte) (x >> 0);  
  89.     }  
  90.   
  91.     /** 
  92.      * 通过byte数组取到long 
  93.      *  
  94.      * @param bb 
  95.      * @param index 
  96.      * @return 
  97.      */  
  98.     public static long getLong(byte[] bb, int index) {  
  99.         return ((((long) bb[index + 7] & 0xff) << 56)  
  100.                 | (((long) bb[index + 6] & 0xff) << 48)  
  101.                 | (((long) bb[index + 5] & 0xff) << 40)  
  102.                 | (((long) bb[index + 4] & 0xff) << 32)  
  103.                 | (((long) bb[index + 3] & 0xff) << 24)  
  104.                 | (((long) bb[index + 2] & 0xff) << 16)  
  105.                 | (((long) bb[index + 1] & 0xff) << 8) | (((long) bb[index + 0] & 0xff) << 0));  
  106.     }  
  107.   
  108.     /** 
  109.      * 字符到字节转换 
  110.      *  
  111.      * @param ch 
  112.      * @return 
  113.      */  
  114.     public static void putChar(byte[] bb, char ch, int index) {  
  115.         int temp = (int) ch;  
  116.         // byte[] b = new byte[2];  
  117.         for (int i = 0; i < 2; i ++ ) {  
  118.             bb[index + i] = new Integer(temp & 0xff).byteValue(); // 将最高位保存在最低位  
  119.             temp = temp >> 8// 向右移8位  
  120.         }  
  121.     }  
  122.   
  123.     /** 
  124.      * 字节到字符转换 
  125.      *  
  126.      * @param b 
  127.      * @return 
  128.      */  
  129.     public static char getChar(byte[] b, int index) {  
  130.         int s = 0;  
  131.         if (b[index + 1] > 0)  
  132.             s += b[index + 1];  
  133.         else  
  134.             s += 256 + b[index + 0];  
  135.         s *= 256;  
  136.         if (b[index + 0] > 0)  
  137.             s += b[index + 1];  
  138.         else  
  139.             s += 256 + b[index + 0];  
  140.         char ch = (char) s;  
  141.         return ch;  
  142.     }  
  143.   
  144.     /** 
  145.      * float转换byte 
  146.      *  
  147.      * @param bb 
  148.      * @param x 
  149.      * @param index 
  150.      */  
  151.     public static void putFloat(byte[] bb, float x, int index) {  
  152.         // byte[] b = new byte[4];  
  153.         int l = Float.floatToIntBits(x);  
  154.         for (int i = 0; i < 4; i++) {  
  155.             bb[index + i] = new Integer(l).byteValue();  
  156.             l = l >> 8;  
  157.         }  
  158.     }  
  159.   
  160.     /** 
  161.      * 通过byte数组取得float 
  162.      *  
  163.      * @param bb 
  164.      * @param index 
  165.      * @return 
  166.      */  
  167.     public static float getFloat(byte[] b, int index) {  
  168.         int l;  
  169.         l = b[index + 0];  
  170.         l &= 0xff;  
  171.         l |= ((long) b[index + 1] << 8);  
  172.         l &= 0xffff;  
  173.         l |= ((long) b[index + 2] << 16);  
  174.         l &= 0xffffff;  
  175.         l |= ((long) b[index + 3] << 24);  
  176.         return Float.intBitsToFloat(l);  
  177.     }  
  178.   
  179.     /** 
  180.      * double转换byte 
  181.      *  
  182.      * @param bb 
  183.      * @param x 
  184.      * @param index 
  185.      */  
  186.     public static void putDouble(byte[] bb, double x, int index) {  
  187.         // byte[] b = new byte[8];  
  188.         long l = Double.doubleToLongBits(x);  
  189.         for (int i = 0; i < 4; i++) {  
  190.             bb[index + i] = new Long(l).byteValue();  
  191.             l = l >> 8;  
  192.         }  
  193.     }  
  194.   
  195.     /** 
  196.      * 通过byte数组取得float 
  197.      *  
  198.      * @param bb 
  199.      * @param index 
  200.      * @return 
  201.      */  
  202.     public static double getDouble(byte[] b, int index) {  
  203.         long l;  
  204.         l = b[0];  
  205.         l &= 0xff;  
  206.         l |= ((long) b[1] << 8);  
  207.         l &= 0xffff;  
  208.         l |= ((long) b[2] << 16);  
  209.         l &= 0xffffff;  
  210.         l |= ((long) b[3] << 24);  
  211.         l &= 0xffffffffl;  
  212.         l |= ((long) b[4] << 32);  
  213.         l &= 0xffffffffffl;  
  214.         l |= ((long) b[5] << 40);  
  215.         l &= 0xffffffffffffl;  
  216.         l |= ((long) b[6] << 48);  
  217.         l &= 0xffffffffffffffl;  
  218.         l |= ((long) b[7] << 56);  
  219.         return Double.longBitsToDouble(l);  
  220.     }  
  221. }  
[java] view plaincopy
  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. }  
0 0
原创粉丝点击