Before the project, in the socket communication needs to pass the value of type int, but in Java Outputsteam seemingly can not directly transmit int type, can only pass byte[], so here to record int and byte[] The method of mutual turn.
/**
* int turn byte[] * * * public
static byte[] inttobytes (int i) {
byte[] bytes = new Byte[4];
Bytes[0] = (byte) (I & 0xff);
BYTES[1] = (byte) ((I >> 8) & 0xff);
BYTES[2] = (byte) ((I >>) & 0xff);
BYTES[3] = (byte) ((I >>) & 0xff);
return bytes;
}
You can turn it back when you receive it.
/**
* byte[] int *
/public static int bytestoint (byte[] bytes) {
int i;
i = (int) ((Bytes[0] & 0xff) | ((Bytes[1] & 0xff) << 8)
| ((Bytes[2] & 0xff) << 16) | ((Bytes[3] & 0xff) <<);
return i;
}
The above is a small set to introduce in the Java int and byte[] mutual conversion, I hope to help you, if you have any questions please give me a message, small series will promptly reply to everyone. Here also thank you very much for the cloud Habitat Community website support!