文章目录
ByteExchanger
单个字节(byte)是8位二进制,能表示的最大的无符号整数为2的8次方-1,即255(11111111),而2位十六进制数能表示的最大值FFH(11111111)与其相同,因此可以用2位16进制数来表示一个字节从0~255的所有内容,据此实现String和字节数组的转化(基于16进制表示的String)。
同理,int类型可由4位byte来表示,long类型可由8位byte来表示,也实现了byte数组和int,以及long型整数的转化。
/**字节转换类,提供包含字节数组向int,long,String(0-F)的互相转化。
* @author dingxiang
*
*/
public class ByteExchanger {
public static final String HEX_STR="0123456789ABCDEF";
public ByteExchanger() {
}
/**单个字节转化为string
* byte是八位二进制,高四位和第四位分别转化为16进制的一个字符,然后拼接在一起。
* @param value
* @return
*/
public static String ByteToString(byte value) {
return ""+HEX_STR.charAt((value>>4)&0x0F)
+HEX_STR.charAt(value&0x0F);
}
/**字节数组转换为string
*
* @param value
* @return
*/
public static String BytesToString(byte[] value) {
StringBuffer str=new StringBuffer();
for (int i = 0; i < value.length; i++) {
str.append(ByteToString(value[i]));
}
return str.toString();
}
/**字符串转化为单个字节
* 字符串的第一位表示字节的高四位对应的字符,第二位表示字节的低四位对应的字符
* 因此根据HEX_STR取出字符串的第一位表示的字符对应的下标<<4(*16),
* 再根据HEX_STR取出字符串的第二位字符对应的下标,
* 再相加,即得到字节转化前的整数。
* @param str
* @return
*/
public static Byte StringToByte(String str) {
if (str == null || str.length() > 2) {
return null;
}
str = str.length() == 1 ? ("0" + str) : str;
byte res = 0;
res += (byte) (HEX_STR.indexOf(str.substring(0, 1)) << 4);
res += (byte) HEX_STR.indexOf(str.substring(1, 2));
return res;
}
/**字符串转化为字节数组。
* @param str
* @return
*/
public static byte[] StringToBytes(String str) {
int len=str.length();
if (len%2!=0) {
return null;
}
byte[] res=new byte[len/2];
for (int i = 0; i < res.length; i++) {
res[i]=StringToByte(str.substring(2*i, 2*i+2));
}
return res;
}
/**字节数组,转化为Int。
* 从字节数组的开始到结束,每次取一个字节放到从int的32位空间由低到高的8位上。
* @param bytes
* @return
* 以字节数组[1,1,0,0]举例
* i=0,byte[0]=1, 左移0*8位&0xFF,即00000001=(00 00 00 01H)=1
* i=1,byte[1]=1, 左移1*8位&0xFF00,即00000001 00000000=(00 00 01 00H)=256
* ...
* 以此类推,将结果相加即得转化出的int=1+256+0+0=257
*/
public static int BytesToInt(byte[] bytes) {
if (bytes==null||bytes.length!=4) {
return -1;
}
int temp = 0xFF;
int res=0;
for (int i = 0; i < 4; i++) {
res|=(bytes[i]<<(i*8)&temp);
temp<<=8;
}
return res;
}
/**int向字节数组的转化
* 从低位到高位,每次截取8位放到字节数组。
* @param value
* @return
* 以int 257(00 00 01 01)H为例
*i=0,res[0]=(byte)((00 00 01 01)H>>0=(00 00 01 01)H&0xFF)=1;
*i=1,res[1]=(byte)((00 00 01 01)H>>8=(00 00 00 01)H&0xFF)=1;
*...
*以此类推
*最后,257转化的byte数组为{1,1,0,0}
*/
public static byte[] IntToBytes(int value) {
int len=4;
byte[] res=new byte[len];
for (int i = 0; i < res.length; i++) {
res[i]=(byte)(value>>(i*8)&0xFF);
}
return res;
}
/**以int向字节数组转化同理
* long向字节数组的转化
* @param value
* @return
*/
public static byte[] LongToBytes(long value) {
int len = 8;
byte[] result = new byte[len];
for (int i = 0; i < len; i++) {
result[i] = (byte) ((value >> (i*8)) & 0xFF);
}
return result;
}
/**以字节数组向int转化同理
* 字节数组转化为long
* @param bytes
* @return
*/
public static long BytesToLong(byte[] bytes) {
if (bytes==null||bytes.length!=8) {
return -1;
}
long res = 0;
long tmp = 0xFF;
for (int i = 0; i < 8; i++) {
res|= (((bytes[i]) << (i*8)) & tmp);
tmp <<= 8;
}
return res;
}
/**从给定的偏移地址offset和数组value,取len个的字节
* @param value
* @param offset
* @param len
* @return
*/
public static byte[] GetBytesAt(byte[] value, int offset, int len) {
byte[] result = new byte[len];
for (int i = 0; i < len; i++) {
result[i] = value[offset + i];
}
return result;
}
/**从给定的偏移地址offset和数组resource,
* 将resource数组偏移地址为offset以后的字节加入到target数组
* @param target
* @param offset
* @param resource
* @return
*/
public static byte[] SetBytesAt(byte[] target, int offset, byte[] resource) {
for (int i = 0; i < resource.length; i++) {
target[offset + i] = resource[i];
}
return target;
}
}