Int String Byte Ip相互変換
4087 ワード
// int byte[]
public static byte[] intToBytes(int value) {
byte[] src = new byte[4];
src[3] = (byte) ((value >> 24) & 0xFF);
src[2] = (byte) ((value >> 16) & 0xFF);
src[1] = (byte) ((value >> 8) & 0xFF);
src[0] = (byte) (value & 0xFF);
return src;
}
// long byte[]
public static byte[] long2bytes(long num) {
byte[] b = new byte[8];
for (int i = 0; i < 8; i++) {
b[i] = (byte) (num >>> (56 - (i * 8)));
}
return b;
}
// byte[] byte[]
public static byte[] byteMerger(byte[] byte_1, byte[] byte_2) {
byte[] byte_3 = new byte[byte_1.length + byte_2.length];
System.arraycopy(byte_1, 0, byte_3, 0, byte_1.length);
System.arraycopy(byte_2, 0, byte_3, byte_1.length, byte_2.length);
return byte_3;
}
// byte[] String
public static String byte2String(byte[] buffer) {
String h = "";
for (int i = 0; i < buffer.length; i++) {
String temp = Integer.toHexString(buffer[i] & 0xFF);
if (temp.length() == 1) {
temp = "0" + temp;
}
h = h + " " + temp;
}
return h;
}
// byte[] int
public static int byte2Int(byte[] bRefArr) {
int iOutcome = 0;
byte bLoop;
for (int i = 0; i < bRefArr.length; i++) {
bLoop = bRefArr[i];
iOutcome += (bLoop & 0xFF) << (8 * i);
}
return iOutcome;
}
// byte[] long
public static long bytes2long(byte[] bb) {
return ((((long) bb[0] & 0xff) << 56) | (((long) bb[1] & 0xff) << 48)
| (((long) bb[2] & 0xff) << 40) | (((long) bb[3] & 0xff) << 32)
| (((long) bb[4] & 0xff) << 24) | (((long) bb[5] & 0xff) << 16)
| (((long) bb[6] & 0xff) << 8) | (((long) bb[7] & 0xff) << 0));
}
// new byte 0
public static byte[] newByteZero(int len) {
byte[] realBuf = new byte[len];
java.util.Arrays.fill(realBuf, (byte) 0x0);
return realBuf;
}
// byte[] 0
public static byte[] byteFillZero(byte[] buf, int len) {
int i;
byte[] realBuf = newByteZero(len);
for (i = 0; i < buf.length; i++) {
realBuf[i] = buf[i];
}
return realBuf;
}
// byte[] 2 stringByte[] 0
public static byte[] byte2StringByte(byte[] b1) {
int i;
for (i = 0; i < b1.length; i++) {
if (b1[i] == 0) {
break;
}
}
return Arrays.copyOfRange(b1, 0, i);
}
// String 0
public static byte[] byteAdd0(byte[] str) {
byte[] b0 = { 0 };
byte[] all = UtilTools.byteMerger(str, b0);
return all;
}
// ip netmask 2 netip
public static String ipNetMask2ip(String ip, String netMask) {
String netIp = "";
String[] ipsz = ip.split("\\.");
String[] netMasksz = netMask.split("\\.");
for (int i = 0; i < ipsz.length; i++) {
int cii = Integer.parseInt(ipsz[i])
& Integer.parseInt(netMasksz[i]);
netIp = netIp + cii;
if (i < ipsz.length - 1) {
netIp += ".";
}
}
return netIp;
}
// int String(ip4)
public static String int2Ip4(int ip) {
return (ip & 0xFF) + "." + ((ip >> 8) & 0xFF) + "."
+ ((ip >> 16) & 0xFF) + "." + ((ip >> 24) & 0xFF);
}
// byte[] String(ip6)
public static String byte2Ip6(byte[] ip) {
String ipv6 = "";
String value;
for (int i = 0; i < ConfigPub.CONFIG_IP6_LEN; i++) {
if ((0 == i % 2) && (i != 0)) {
ipv6 += ":";
}
value = Integer.toHexString(ip[i] & 0xff);
ipv6 += value;
}
return ipv6;
}
// mac byte[]
public static byte[] mac2Bytes(String mac) {
byte[] macBytes = new byte[6];
String[] strArr = mac.split(":");
for (int i = 0; i < strArr.length; i++) {
int value = Integer.parseInt(strArr[i], 16);
macBytes[i] = (byte) value;
}
return macBytes;
}
// ip4 byte
public static byte[] ip42Bytes(String ipAdd) {
byte[] binIP = new byte[4];
String[] strs = ipAdd.split("\\.");
for (int i = 0; i < strs.length; i++) {
binIP[i] = (byte) Integer.parseInt(strs[i]);
}
return binIP;
}