热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

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

文章转载自http:blog.csdn.netleetcworksarticledetails73907311packagecom.util;23**4*5*6*文

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

1 package com.util;
2
3 /**
4 *
5 *


    6 *
  • 文件名称: com.born.util.ByteUtil.java

  • 7 *
  • 文件描述: byte转换工具

  • 8 *
  • 版权所有: 版权所有(C)2001-2006

  • 9 *
  • 公 司: bran

  • 10 *
  • 内容摘要:

  • 11 *
  • 其他说明:

  • 12 *
  • 完成日期:2011-7-18

  • 13 *
  • 修改记录0:无

  • 14 *

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 &#43; 1] <<8) | b[index &#43; 0] & 0xff));
43 }
44
45 /**
46 * 转换int为byte数组
47 *
48 * &#64;param bb
49 * &#64;param x
50 * &#64;param index
51 */
52 public static void putInt(byte[] bb, int x, int index) {
53 bb[index &#43; 3] &#61; (byte) (x >> 24);
54 bb[index &#43; 2] &#61; (byte) (x >> 16);
55 bb[index &#43; 1] &#61; (byte) (x >> 8);
56 bb[index &#43; 0] &#61; (byte) (x >> 0);
57 }
58
59 /**
60 * 通过byte数组取到int
61 *
62 * &#64;param bb
63 * &#64;param index
64 * 第几位开始
65 * &#64;return
66 */
67 public static int getInt(byte[] bb, int index) {
68 return (int) ((((bb[index &#43; 3] & 0xff) <<24)
69 | ((bb[index &#43; 2] & 0xff) <<16)
70 | ((bb[index &#43; 1] & 0xff) <<8) | ((bb[index &#43; 0] & 0xff) <<0)));
71 }
72
73 /**
74 * 转换long型为byte数组
75 *
76 * &#64;param bb
77 * &#64;param x
78 * &#64;param index
79 */
80 public static void putLong(byte[] bb, long x, int index) {
81 bb[index &#43; 7] &#61; (byte) (x >> 56);
82 bb[index &#43; 6] &#61; (byte) (x >> 48);
83 bb[index &#43; 5] &#61; (byte) (x >> 40);
84 bb[index &#43; 4] &#61; (byte) (x >> 32);
85 bb[index &#43; 3] &#61; (byte) (x >> 24);
86 bb[index &#43; 2] &#61; (byte) (x >> 16);
87 bb[index &#43; 1] &#61; (byte) (x >> 8);
88 bb[index &#43; 0] &#61; (byte) (x >> 0);
89 }
90
91 /**
92 * 通过byte数组取到long
93 *
94 * &#64;param bb
95 * &#64;param index
96 * &#64;return
97 */
98 public static long getLong(byte[] bb, int index) {
99 return ((((long) bb[index &#43; 7] & 0xff) <<56)
100 | (((long) bb[index &#43; 6] & 0xff) <<48)
101 | (((long) bb[index &#43; 5] & 0xff) <<40)
102 | (((long) bb[index &#43; 4] & 0xff) <<32)
103 | (((long) bb[index &#43; 3] & 0xff) <<24)
104 | (((long) bb[index &#43; 2] & 0xff) <<16)
105 | (((long) bb[index &#43; 1] & 0xff) <<8) | (((long) bb[index &#43; 0] & 0xff) <<0));
106 }
107
108 /**
109 * 字符到字节转换
110 *
111 * &#64;param ch
112 * &#64;return
113 */
114 public static void putChar(byte[] bb, char ch, int index) {
115 int temp &#61; (int) ch;
116 // byte[] b &#61; new byte[2];
117 for (int i &#61; 0; i <2; i &#43;&#43; ) {
118 bb[index &#43; i] &#61; new Integer(temp & 0xff).byteValue(); // 将最高位保存在最低位
119 temp &#61; temp >> 8; // 向右移8位
120 }
121 }
122
123 /**
124 * 字节到字符转换
125 *
126 * &#64;param b
127 * &#64;return
128 */
129 public static char getChar(byte[] b, int index) {
130 int s &#61; 0;
131 if (b[index &#43; 1] > 0)
132 s &#43;&#61; b[index &#43; 1];
133 else
134 s &#43;&#61; 256 &#43; b[index &#43; 0];
135 s *&#61; 256;
136 if (b[index &#43; 0] > 0)
137 s &#43;&#61; b[index &#43; 1];
138 else
139 s &#43;&#61; 256 &#43; b[index &#43; 0];
140 char ch &#61; (char) s;
141 return ch;
142 }
143
144 /**
145 * float转换byte
146 *
147 * &#64;param bb
148 * &#64;param x
149 * &#64;param index
150 */
151 public static void putFloat(byte[] bb, float x, int index) {
152 // byte[] b &#61; new byte[4];
153 int l &#61; Float.floatToIntBits(x);
154 for (int i &#61; 0; i <4; i&#43;&#43;) {
155 bb[index &#43; i] &#61; new Integer(l).byteValue();
156 l &#61; l >> 8;
157 }
158 }
159
160 /**
161 * 通过byte数组取得float
162 *
163 * &#64;param bb
164 * &#64;param index
165 * &#64;return
166 */
167 public static float getFloat(byte[] b, int index) {
168 int l;
169 l &#61; b[index &#43; 0];
170 l &&#61; 0xff;
171 l |&#61; ((long) b[index &#43; 1] <<8);
172 l &&#61; 0xffff;
173 l |&#61; ((long) b[index &#43; 2] <<16);
174 l &&#61; 0xffffff;
175 l |&#61; ((long) b[index &#43; 3] <<24);
176 return Float.intBitsToFloat(l);
177 }
178
179 /**
180 * double转换byte
181 *
182 * &#64;param bb
183 * &#64;param x
184 * &#64;param index
185 */
186 public static void putDouble(byte[] bb, double x, int index) {
187 // byte[] b &#61; new byte[8];
188 long l &#61; Double.doubleToLongBits(x);
189 for (int i &#61; 0; i <4; i&#43;&#43;) {
190 bb[index &#43; i] &#61; new Long(l).byteValue();
191 l &#61; l >> 8;
192 }
193 }
194
195 /**
196 * 通过byte数组取得float
197 *
198 * &#64;param bb
199 * &#64;param index
200 * &#64;return
201 */
202 public static double getDouble(byte[] b, int index) {
203 long l;
204 l &#61; b[0];
205 l &&#61; 0xff;
206 l |&#61; ((long) b[1] <<8);
207 l &&#61; 0xffff;
208 l |&#61; ((long) b[2] <<16);
209 l &&#61; 0xffffff;
210 l |&#61; ((long) b[3] <<24);
211 l &&#61; 0xffffffffl;
212 l |&#61; ((long) b[4] <<32);
213 l &&#61; 0xffffffffffl;
214 l |&#61; ((long) b[5] <<40);
215 l &&#61; 0xffffffffffffl;
216 l |&#61; ((long) b[6] <<48);
217 l &&#61; 0xffffffffffffffl;
218 l |&#61; ((long) b[7] <<56);
219 return Double.longBitsToDouble(l);
220 }
221 }


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


推荐阅读
author-avatar
捕鱼达人2602917825
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有