16进制字符串转2进制
2进制转16进制字符串
查找byte数组中存在的重复连续的位置第一个
public class Main1 {
public static void main(String[] args) {
System.out.println("Hello world! ");
}
public static int indexOf(byte[] source, byte[] dest) {
if (null == source || source.length <= 0 || null == dest || dest.length <= 0) {
return -1;
}
int sourceLen = source.length;
int destLen = dest.length;
if (sourceLen < destLen) {
return -1;
}
for (int i = 0; i <= sourceLen - destLen; i++) {
int index = i;
for (int j = 0; j < destLen; j++) {
byte bt = source[j + i];
if (bt != dest[j]) {
index = -1;
break;
}
}
if (index != -1) {
return index;
}
}
return -1;
}
public static int sumByte(byte[] buff) {
int sum = 0;
if (null == buff || buff.length <= 0) {
return sum;
}
for (byte b : buff) {
sum += byte2int(b);
}
return sum & 0xFF;
}
public static int byte2int(byte b) {
return b < 0 ? 256 + b : b;
}
public static String byte2hexStr(byte b) {
String s = Integer.toHexString(0xFF & b);
if (s.length() < 2) {
s = "0" + s;
}
return s.toUpperCase();
}
public static String byte2hexStr(byte[] b) {
if (null == b || b.length <= 0) {
return "";
}
StringBuilder sb = new StringBuilder(b.length);
for (byte value : b) {
sb.append(byte2hexStr(value));
}
return sb.toString();
}
public static byte[] hexStr2byte(String hex) {
byte[] b = null;
if (null == hex || hex.length() <= 0) {
return b;
}
hex = hex.toUpperCase();
int len = hex.length() / 2;
char[] hexChars = hex.toCharArray();
b = new byte[len];
for (int i = 0; i < len; i++) {
int pos = i * 2;
b[i] = (byte) (char2byte(hexChars[pos]) << 4 | char2byte(hexChars[pos + 1]));
}
return b;
}
public static byte char2byte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
}
public static byte[] setLen(int len) {
byte[] buff = new byte[2];
buff[0] = (byte) (len / 256);
buff[1] = (byte) (len % 256);
return buff;
}
public static int getLen(byte l, byte r) {
return byte2int(l) * 256 + byte2int(r);
}
}