byte和hexstring,int,string等的转换类

时间:2023-03-08 22:31:29
byte和hexstring,int,string等的转换类
public class HexConversion {

	/**
* 16进制数的字符串转字节数组(16进制转字节数组)
*
* @param hexString
* 16进制字符串
* @return 字节数组
*/
public static byte[] hexStringToBytes(String hexString) {
if (hexString == null || hexString.equals("")) {
return null;
}
hexString = hexString.toUpperCase();
int length = hexString.length() / 2;
char[] hexChars = hexString.toCharArray();
byte[] d = new byte[length];
for (int i = 0; i < length; i++) {
int pos = i * 2;
d[i] = (byte) (charToByte(hexChars[pos]) << 4 | charToByte(hexChars[pos + 1]));
}
return d;
} public static String bytesToString(byte[] bytes){
int size=0;
for(int i=0;i<bytes.length;i++){
if(bytes[i]!=0){
size++;
}else{
break;
}
}
byte[] data=new byte[size];
System.arraycopy(bytes, 0, data, 0, size);
return new String(data);
} /**
* 字符转字节
*
* @param c
* 字符
* @return byte 字节
*/
private static byte charToByte(char c) {
return (byte) "0123456789ABCDEF".indexOf(c);
} /**
* 字节转16进制字符串
*
* @param src
* 字节数组
* @return 16进制字符串
*/
public static String bytesToHexString(byte[] src) {
StringBuilder stringBuilder = new StringBuilder("");
if (src == null || src.length <= 0) {
return null;
}
for (int i = 0; i < src.length; i++) {
int v = src[i] & 0xFF;
String hv = Integer.toHexString(v);
if (hv.length() < 2) {
stringBuilder.append(0);
}
stringBuilder.append(hv);
}
return stringBuilder.toString();
} /**
* 字节转整型
*
* @param b 字节数组
* @return int型
*/
public static int byte2Int(byte[] b) {
int intValue = 0;
for (int i = 0; i < b.length; i++) {
intValue += (b[i] & 0xFF) << (8 * (3 - i));
//System.out.print("intvalue --- "+intValue+" ");
}
return intValue;
} public static long dowrd2Long(byte[] bytes) {
long unsignedLong=0;
for(int i=0;i<bytes.length;i++){
unsignedLong=(long)(unsignedLong|(0x000000FF & ((int) bytes[i]))<<(8*i));
}
return unsignedLong;
} public static long byteTranLong(byte[] b) {
long value = 0;
for (int i = 0; i < b.length; i++) {
value += (b[i] & 0xFF) << (8 * (3 - i));
}
return value;
} /**
* 整形转字节数组
*
* @param num
* 整形
* @return 字节数组
*/
public static byte[] int2bytes(int num) {
byte[] b = new byte[4];
//int mask = 0xff;
for (int i = 0; i < 4; i++) {
b[i] = (byte) (num >>> (24 - i * 8));
}
return b;
} /**
* 把4字节的数组转换成IP
*
* @param bytes
* 4个字节的字节数组
* @return 字符串IP地址
*/
// public static String bytes2Ip(byte[] bytes) {
// String ip = "";
// for (int i = 0; i < 4; i++) {
// String tmp = String.valueOf(bytes[i]);
// if (bytes[i] < 0) {
// tmp = String.valueOf(127+Math.abs(bytes[i]));
// }
//
// if (i < 3) {
// ip += tmp + ".";
// } else {
// ip += tmp;
//
// }
// }
// return ip;
// }
public static String bytes2Ip(byte[] bytes) {
String ip = "";
for (int i=0;i<bytes.length;i++) {
int inval = 0;
inval += (bytes[i] & 0xFF);
ip += inval + ".";
}
ip = ip.substring(0, ip.length() - 1);
return ip;
} /**
* IP地址转换字节数组
*
* @param ip
* 字符串
* @return 字节数组
*/
public static byte[] ip2Bytes(String ip) {
String[] ips = ip.split("[.]");
byte[] ipbs = new byte[4];
// IP地址压缩成4字节,如果要进一步处理的话,就可以转换成一个int了.
int j = 0;
for (int i =0;i<ipbs.length;i++) {
int m = Integer.parseInt(ips[j]);
ipbs[i] = (byte) (m & 0xFF);
j++;
// byte b = (byte) m;
// if (m > 127) {
// b = (byte) (127 - m);
// }
// ipbs[i] = b;
}
return ipbs;
} /**
* 字节数组转字符串
*
* @param bytes
* 字节数组/不足位补0的字节数组
* @return 字符串
*/
public static String bytes2String(byte[] bytes) {
int beginIndex = 0;
int endIndex=0;
int length=0;
// 判断是否补过0
for (int i = 0; i < bytes.length; i++) {
if (bytes[i] != 0) {
beginIndex=i;
break;
}
}
for (int i = bytes.length-1; i >=0; i--) {
if (bytes[i] != 0) {
endIndex=i;
break;
}
}
if(endIndex<=beginIndex){
length=0;
}else{
length=endIndex-beginIndex+1;
}
if(length==0){
return "";
}
byte[] b=new byte[length];
System.arraycopy(bytes, beginIndex, b, 0, length);
return new String(b);
} public static void main(String[] args) { }
}