📄 change.java
字号:
package com.stun.tool;
public class Change {
/**
* 把一个整形该为byte
* @param value
* @return
* @throws Exception
*/
public byte integerToOneByte(int value) throws Exception {
if ((value > Math.pow(2, 15)) || (value < 0)) {
throw new Exception("Integer value " + value
+ " is larger than 2^15");
}
return (byte) (value & 0xFF);
}
/**
* 把一个整形改为2位的byte数组
* @param value
* @return
* @throws Exception
*/
public byte[] integerToTwoBytes(int value) throws Exception {
byte[] result = new byte[2];
if ((value > Math.pow(2, 31)) || (value < 0)) {
throw new Exception("Integer value " + value
+ " is larger than 2^31");
}
result[0] = (byte) ((value >>> 8) & 0xFF);
result[1] = (byte) (value & 0xFF);
return result;
}
/**
* 把一个整形改为4位的byte数组
* @param value
* @return
* @throws Exception
*/
public byte[] integerToFourBytes(int value) throws Exception {
byte[] result = new byte[4];
if ((value > Math.pow(2, 63)) || (value < 0)) {
throw new Exception("Integer value " + value
+ " is larger than 2^63");
}
result[0] = (byte) ((value >>> 24) & 0xFF);
result[1] = (byte) ((value >>> 16) & 0xFF);
result[2] = (byte) ((value >>> 8) & 0xFF);
result[3] = (byte) (value & 0xFF);
return result;
}
/**
* 把一个byte转化位整形
* @param value
* @return
* @throws Exception
*/
public int oneByteToInteger(byte value) throws Exception {
return (int) value & 0xFF;
}
/**
* 把一个2位的数组转化位整形
* @param value
* @return
* @throws Exception
*/
public int twoBytesToInteger(byte[] value) throws Exception {
if (value.length < 2) {
throw new Exception("Byte array too short!");
}
int temp0 = value[0] & 0xFF;
int temp1 = value[1] & 0xFF;
return ((temp0 << 8) + temp1);
}
/**
* 把一个4位的数组转化位整形
* @param value
* @return
* @throws Exception
*/
public long fourBytesToLong(byte[] value) throws Exception {
if (value.length < 4) {
throw new Exception("Byte array too short!");
}
int temp0 = value[0] & 0xFF;
int temp1 = value[1] & 0xFF;
int temp2 = value[2] & 0xFF;
int temp3 = value[3] & 0xFF;
return (((long) temp0 << 24) + (temp1 << 16) + (temp2 << 8) + temp3);
}
/**
* 得到一个消息ID
* @return
* @throws Exception
*/
public byte[] generateTransactionID() throws Exception {
byte[] id = new byte[16];
System.arraycopy(integerToTwoBytes((int) (Math.random() * 65536)), 0,
id, 0, 2);
System.arraycopy(integerToTwoBytes((int) (Math.random() * 65536)), 0,
id, 2, 2);
System.arraycopy(integerToTwoBytes((int) (Math.random() * 65536)), 0,
id, 4, 2);
System.arraycopy(integerToTwoBytes((int) (Math.random() * 65536)), 0,
id, 6, 2);
System.arraycopy(integerToTwoBytes((int) (Math.random() * 65536)), 0,
id, 8, 2);
System.arraycopy(integerToTwoBytes((int) (Math.random() * 65536)), 0,
id, 10, 2);
System.arraycopy(integerToTwoBytes((int) (Math.random() * 65536)), 0,
id, 12, 2);
System.arraycopy(integerToTwoBytes((int) (Math.random() * 65536)), 0,
id, 14, 2);
return id;
}
/**
* 把IP拆分位int数组
* @param ip
* @return
* @throws Exception
*/
public int[] getIntIPValue(String ip) throws Exception {
String[] sip = ip.split("[.]");
if (sip.length != 4) {
throw new Exception("error IPAddress");
}
int[] intIP = { Integer.parseInt(sip[0]), Integer.parseInt(sip[1]),
Integer.parseInt(sip[2]), Integer.parseInt(sip[3]) };
return intIP;
}
/**
* 把byte类型IP地址转化位字符串
* @param address
* @return
* @throws Exception
*/
public String getStringIPValue(byte[] address) throws Exception {
if (address.length != 4) {
throw new Exception("error IPaddress");
}
int first = this.oneByteToInteger(address[0]);
int second = this.oneByteToInteger(address[1]);
int third = this.oneByteToInteger(address[2]);
int fourth = this.oneByteToInteger(address[3]);
return first + "." + second + "." + third + "." + fourth;
}
/**
* 读取消息地址
* @param message
* @param index
* @param port
* @param address
*/
public void getAddress(byte[] message, int index, byte[] port,
byte[] address) {
System.arraycopy(message, index + 22,
port, 0, 2);
System.arraycopy(message, index + 24,
address, 0, 4);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -