[JAVA]int类型和byte[]互转

package com.mapabc.sz_hbt.util; /** *

Title:整型与长度为4的字节数组的互换

*

Description:

*

Copyright: Copyright (c) 2007-5-10

*

Company: www.mapabc.com

* @author luoyj * @version 1.0 */ public class ByteUtil { /** * 整型转换为4位字节数组 * @param intValue * @return */ public static byte[] int2Byte(int intValue) { byte[] b = new byte[4]; for (int i = 0; i < 4; i++) { b[i] = (byte) (intValue >> 8 * (3 - i) & 0xFF); //System.out.print(Integer.toBinaryString(b[i])+" "); //System.out.print((b[i] & 0xFF) + " "); } return b; } /** * 4位字节数组转换为整型 * @param b * @return */ public static int byte2Int(byte[] b) { int intValue = 0; // int tempValue = 0xFF; for (int i = 0; i < b.length; i++) { intValue += (b[i] & 0xFF) << (8 * (3 - i)); // System.out.print(Integer.toBinaryString(intValue)+" "); } return intValue; } }

你可能感兴趣的:(byte,java,class,c)