Mutual conversion of plastic variables and byte arrays

xiaoxiao2021-03-06  78

I encountered a conversion when I read and wrote socket today. To convert {Khan ~ company defined packets between INT variables and byte arrays} as IS known to all, Java intimate 4-byte storage, So I think of the implementation of the shift operation, and it can also be restored to the INT variable by shift operation. The specific code is as follows: ---------------------------------------------- ------------------------------- / * c 2004-11-5 * * Todo to change the template for this GENERATED FILE Go To * Window - Preferences - Java - Code Style - Code Templates * / Package Converter;

/ ** * @author NetSniffer * * TODO To change the template for this generated type comment go to * Window - Preferences - Java - Code Style - Code Templates * / public class IntByteConvertor {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;} public static int Byte2Int (Byte [] b) {int INTVALUE = 0; for (int I = 0; i

转载请注明原文地址:https://www.9cbs.com/read-95662.html

New Post(0)