admin 管理员组

文章数量: 887021


2024年1月10日发(作者:python窗口界面)

15.

16. /**

17. * 字节数组到short的转换.

18. */

19. public static short byteToShort(byte[] b) {

20. short s = 0;

21. short s0 = (short) (b[0] & 0xff);// 最低位

22. short s1 = (short) (b[1] & 0xff);

23. s1 <<= 8;

24. s = (short) (s0 | s1);

25. return s;

26. }

27.

28.

29. /**

30. * int到字节数组的转换.

31. */

32. public static byte[] intToByte(int number) {

33. int temp = number;

34. byte[] b = new byte[4];

35. for (int i = 0; i < ; i++) {

36. b[i] = new Integer(temp & 0xff).byteValue();// 将最低位保存在最低位

37. temp = temp >> 8;// 向右移8位

38. }

39. return b;

40. }

41.

42. /**

43. * 字节数组到int的转换.

44. */

45. public static int byteToInt(byte[] b) {

46. int s = 0;

47. int s0 = b[0] & 0xff;// 最低位

48. int s1 = b[1] & 0xff;

49. int s2 = b[2] & 0xff;

50. int s3 = b[3] & 0xff;

51. s3 <<= 24;

52. s2 <<= 16;

53. s1 <<= 8;

54. s = s0 | s1 | s2 | s3;

55. return s;

56. }

57.

58.

59. /**

60. * long类型转成byte数组

61. */

62. public static byte[] longToByte(long number) {

63. long temp = number;

64. byte[] b = new byte[8];

65. for (int i = 0; i < ; i++) {

66. b[i] = new Long(temp & 0xff).byteValue();// 将最低位保存在最低位 temp = temp

67. // >> 8;// 向右移8位

68. }

69. return b;

70. }

71.

72. /**

73. * 字节数组到long的转换.

74. */

75. public static long byteToLong(byte[] b) {

76. long s = 0;

77. long s0 = b[0] & 0xff;// 最低位

78. long s1 = b[1] & 0xff;

79. long s2 = b[2] & 0xff;

80. long s3 = b[3] & 0xff;

81. long s4 = b[4] & 0xff;// 最低位

82. long s5 = b[5] & 0xff;

83. long s6 = b[6] & 0xff;

84. long s7 = b[7] & 0xff;

85.

86. // s0不变

87. s1 <<= 8;

88. s2 <<= 16;

89. s3 <<= 24;

90. s4 <<= 8 * 4;

91. s5 <<= 8 * 5;

92. s6 <<= 8 * 6;

93. s7 <<= 8 * 7;

94. s = s0 | s1 | s2 | s3 | s4 | s5 | s6 | s7;

95. return s;

96. }

97.

98. /**

99. * double到字节数组的转换.

100. */

101. public static byte[] doubleToByte(double num) {

102. byte[] b = new byte[8];

146. // 4 bytes

147. int accum = 0;

148. for ( int shiftBy = 0; shiftBy < 4; shiftBy++ ) {

149. accum |= (b[shiftBy] & 0xff) << shiftBy * 8;

150. }

151. return sToFloat(accum);

152. }

153.

154. /**

155. * char到字节数组的转换.

156. */

157. public static byte[] charToByte(char c){

158. byte[] b = new byte[2];

159. b[0] = (byte) ((c & 0xFF00) >> 8);

160. b[1] = (byte) (c & 0xFF);

161. return b;

162. }

163.

164. /**

165. * 字节数组到char的转换.

166. */

167. public static char byteToChar(byte[] b){

168. char c = (char) (((b[0] & 0xFF) << 8) | (b[1] & 0xFF));

169. return c;

170. }

171.

172. /**

173. * string到字节数组的转换.

174. */

175. public static byte[] stringToByte(String str) throws UnsupportedEncodingException{

176. return es("GBK");

177. }

178.

179. /**

180. * 字节数组到String的转换.

181. */

182. public static String bytesToString(byte[] str) {

183. String keyword = null;

184. try {

185. keyword = new String(str,"GBK");

186. } catch (UnsupportedEncodingException e) {

187. tackTrace();

188. }

189. return keyword;

189. return keyword;

190. }

191.

192.

193. /**

194. * object到字节数组的转换

195. */

196. @Test

197. public void testObject2ByteArray() throws IOException,

198. ClassNotFoundException {

199. // Object obj = "";

200. Integer[] obj = { 1, 3, 4 };

201.

202. // // object to bytearray

203. ByteArrayOutputStream bo = new ByteArrayOutputStream();

204. ObjectOutputStream oo = new ObjectOutputStream(bo);

205. bject(obj);

206. byte[] bytes = Array();

207. ();

208. ();

209. n(ng(bytes));

210.

211. Integer[] intArr = (Integer[]) testByteArray2Object(bytes);

212. n((intArr));

213.

214.

215. byte[] b2 = intToByte(123);

216. n(ng(b2));

217.

218. int a = byteToInt(b2);

219. n(a);

220.

221. }

222.

223. /**

224. * 字节数组到object的转换.

225. */

226. private Object testByteArray2Object(byte[] bytes) throws IOException,

227. ClassNotFoundException {

228. // byte[] bytes = null;

229. Object obj;

230. // bytearray to object

231. ByteArrayInputStream bi = new ByteArrayInputStream(bytes);

232. ObjectInputStream oi = new ObjectInputStream(bi);

232. ObjectInputStream oi = new ObjectInputStream(bi);

233. obj = ject();

234. ();

235. ();

236. n(obj);

237. return obj;

238. }

239.

240. }


本文标签: 数组 转换 字节 界面 作者