程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> JAVA編程 >> JAVA綜合教程 >> [轉載] java中byte數組與int,long,short間的轉換,byteshort

[轉載] java中byte數組與int,long,short間的轉換,byteshort

編輯:JAVA綜合教程

[轉載] java中byte數組與int,long,short間的轉換,byteshort


文章轉載自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 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 }
  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 }

 

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved