你的位置:首页 > 软件开发 > Java > [转载] java中byte数组与int,long,short间的转换

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

发布时间:2016-07-16 14:00:07
文章转载自http://blog.csdn.net/leetcworks/article/details/7390731 1 package com.util; 2 3 /** 4 * 5 * <ul> 6 * <li>文件名称: com.bor ...

文章转载自http://blog.csdn.net/leetcworks/article/details/7390731

 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 ch112    * @return113   */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 b127    * @return128   */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     else134       s += 256 + b[index + 0];135     s *= 256;136     if (b[index + 0] > 0)137       s += b[index + 1];138     else139       s += 256 + b[index + 0];140     char ch = (char) s;141     return ch;142   }143 144   /**145    * float转换byte146    * 147    * @param bb148    * @param x149    * @param index150   */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数组取得float162    * 163    * @param bb164    * @param index165    * @return166   */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转换byte181    * 182    * @param bb183    * @param x184    * @param index185   */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数组取得float197    * 198    * @param bb199    * @param index200    * @return201   */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 }

 

海外公司注册、海外银行开户、跨境平台代入驻、VAT、EPR等知识和在线办理:https://www.xlkjsw.com

原标题:[转载] java中byte数组与int,long,short间的转换

关键词:JAVA

*特别声明:以上内容来自于网络收集,著作权属原作者所有,如有侵权,请联系我们: admin#shaoqun.com (#换成@)。