public class TypeConvert
{
02. /* 字符串转byte[]
03. 这个方法转换后的结果是会多一些 48字符进来的就是代表的是0不知道为什么,但是可以只是取出指定的字符串就行了
04. */
05. public static byte[] hexStringToBytes(String hexString)
{
if (hexString == null || hexString.equals(""))
{
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
}
17.
18. /* byte转short */
19. public final static short getShort(byte[] buf, boolean asc, int len) {
20. short r = 0;
21. if (asc)
22. for (int i = len - 1; i >= 0; i--) {
23. r <<= 8;
24. r |= (buf[i] & 0x00ff);
25. }
26. else
27. for (int i = 0; i < len; i++) {
28. r <<= 8;
29. r |= (buf[i] & 0x00ff);
30. }
31.
32. return r;
33. }
34.
35. /* B2 -> 0xB2 */
36. public static int stringToByte(String in, byte[] b) throws Exception {
37. if (b.length < in.length() / 2) {
38. throw new Exception("byte array too small");
39. }
40.
41. int j=0;
42. StringBuffer buf = new StringBuffer(2);
43. for (int i=0; i<in.length(); i++, j++) {
44. buf.insert(0, in.charAt(i));
45. buf.insert(1, in.charAt(i+1));
46. int t = Integer.parseInt(buf.toString(),16);
47. System.out.println("byte hex value:" + t);
48. b[j] = (byte)t;
49. i++;
50. buf.delete(0,2);
51. }
52.
53. return j;
54. }
55.
56. /* byte to int */
57. public final static int getInt(byte[] buf, boolean asc, int len) {
58. if (buf == null) {
59. throw new IllegalArgumentException("byte array is null!");
60. }
61. if (len > 4) {
62. throw new IllegalArgumentException("byte array size > 4 !");
63. }
64. int r = 0;
65. if (asc)
66. for (int i = len - 1; i >= 0; i--) {
67. r <<= 8;
68. r |= (buf[i] & 0x000000ff);
69. }
70. else
71. for (int i = 0; i < len; i++) {
72. r <<= 8;
73. r |= (buf[i] & 0x000000ff);
74. }
75. return r;
76. }
77.
78. /* int -> byte[] */
79. public static byte[] intToBytes(int num) {
80. byte[] b = new byte[4];
81. for (int i = 0; i < 4; i++) {
82. b[i] = (byte) (num >>> (24 - i * 8));
83. }
84.
85. return b;
86. }
87.
88. /* short to byte[] */
89. public static byte[] shortToBytes(short num) {
90. byte[] b = new byte[2];
91.
92. for (int i = 0; i < 2; i++) {
93. b[i] = (byte) (num >>> (i * 8));
94. }
95.
96. return b;
97. }
98.
99. /* byte to String */
100. private static char findHex(byte b) {
101. int t = new Byte(b).intValue();
102. t = t < 0 ? t + 16 : t;
103.
104. if ((0 <= t) &&(t <= 9)) {
105. return (char)(t + '0');
106. }
107.
108. return (char)(t-10+'A');
109. }
110. public static String byteToString(byte b) {
111. byte high, low;
112. byte maskHigh = (byte)0xf0;
113. byte maskLow = 0x0f;
114.
115. high = (byte)((b & maskHigh) >> 4);
116. low = (byte)(b & maskLow);
117.
118. StringBuffer buf = new StringBuffer();
119. buf.append(findHex(high));
120. buf.append(findHex(low));
121.
122. return buf.toString();
123. }
124.
125. /* short -> byte */
126. public final static byte[] getBytes(short s, boolean asc) {
127. byte[] buf = new byte[2];
128. if (asc) for (int i = buf.length - 1; i >= 0; i--) { buf[i] = (byte) (s & 0x00ff);
129. s >>= 8;
130. }
131. else
132. for (int i = 0; i < buf.length; i++) {
133. buf[i] = (byte) (s & 0x00ff);
134. s >>= 8;
135. }
136. return buf;
137. }
138. /* int -> byte[] */
139. public final static byte[] getBytes(int s, boolean asc) {
140. byte[] buf = new byte[4];
141. if (asc)
142. for (int i = buf.length - 1; i >= 0; i--) {
143. buf[i] = (byte) (s & 0x000000ff);
144. s >>= 8;
145. }
146. else
147. for (int i = 0; i < buf.length; i++) {
148. buf[i] = (byte) (s & 0x000000ff);
149. s >>= 8;
150. }
151. return buf;
152. }
153.
154. /* long -> byte[] */
155. public final static byte[] getBytes(long s, boolean asc) {
156. byte[] buf = new byte[8];
157. if (asc)
158. for (int i = buf.length - 1; i >= 0; i--) {
159. buf[i] = (byte) (s & 0x00000000000000ff);
160. s >>= 8;
161. }
162. else
163. for (int i = 0; i < buf.length; i++) {
164. buf[i] = (byte) (s & 0x00000000000000ff);
165. s >>= 8;
166. }
167. return buf;
168. }
169.
170. /* byte[]->int */
171. public final static int getInt(byte[] buf, boolean asc) {
172. if (buf == null) {
173. throw new IllegalArgumentException("byte array is null!");
174. }
175. if (buf.length > 4) {
176. throw new IllegalArgumentException("byte array size > 4 !");
177. }
178. int r = 0;
179. if (asc)
180. for (int i = buf.length - 1; i >= 0; i--) {
181. r <<= 8;
182. r |= (buf[i] & 0x000000ff);
183. }
184. else
185. for (int i = 0; i < buf.length; i++) {
186. r <<= 8;
187. r |= (buf[i] & 0x000000ff);
188. }
189. return r;
190. }
191. /* byte[] -> long */
192. public final static long getLong(byte[] buf, boolean asc) {
193. if (buf == null) {
194. throw new IllegalArgumentException("byte array is null!");
195. }
196. if (buf.length > 8) {
197. throw new IllegalArgumentException("byte array size > 8 !");
198. }
199. long r = 0;
200. if (asc)
201. for (int i = buf.length - 1; i >= 0; i--) {
202. r <<= 8;
203. r |= (buf[i] & 0x00000000000000ff);
204. }
205. else
206. for (int i = 0; i < buf.length; i++) {
207. r <<= 8;
208. r |= (buf[i] & 0x00000000000000ff);
209. }
210. return r;
211. }
212.}
数据类型转换
最新推荐文章于 2022-07-20 18:40:26 发布