public class NumberBytesUtils {
public static void main(String[] args) {
byte[] bytes = new byte[4];
bytes[0] = (byte) 65;
bytes[1] = (byte) 0;
bytes[2] = (byte) 97;
bytes[3] = (byte) 0;
System.out.println("String: " + new String(bytes));
int iv = bytesToInt(bytes);
float fv = bytesToFloat(bytes);
byte[] bs = intToBytes(iv);
byte[] fs = floatToBytes(fv);
System.out.println("Int: " + iv);
System.out.println("Float: " + fv);
System.out.println("------------");
for (int i = 0; i < bs.length; i++) {
System.out.println(bs[i]);
}
System.out.println("============");
for (int i = 0; i < fs.length; i++) {
System.out.println(fs[i]);
}
System.out.println("============");
System.out.println(bytesToFloat(floatToBytes(-0.45367f)));
System.out.println("************");
// System.out.println(0xff);
// System.out.println(0xff00);
// System.out.println(0xff0000);
// System.out.println(0xff000000);
byte[] bytesL = new byte[8];
System.arraycopy(bytes, 0, bytesL, 0, bytes.length);
bytesL[4] = (byte) 0;
bytesL[5] = (byte) 0;
bytesL[6] = (byte) 0;
bytesL[7] = (byte) 0;
long lv = bytesToLong(bytesL);
double dv = bytesToDouble(bytesL);
byte[] bls = longToBytes(lv);
byte[] dls = doubleToBytes(dv);
System.out.println("Long: " + lv);
System.out.println("Double: " + dv);
System.out.println("----");
for (int i = 0; i < bls.length; i++) {
System.out.println(bls[i]);
}
System.out.println("----");
for (int i = 0; i < dls.length; i++) {
System.out.println(dls[i]);
}
System.out.println("****");
System.out.println(bytesToDouble(doubleToBytes(2.345)));
}
/**
* bytes[3] = value >> 24
* bytes[2] = value >> 16
* bytes[1] = value >> 8
* bytes[0] = value >> 0
* @param value
* @return
*/
public static byte[] intToBytes(int value){
int length = 4;
byte[] bytes = new byte[length];
for (int i = length - 1; i >= 0; i--) {
int offset = i * 8; // 24, 16, 8
bytes[i] = (byte) (value >> offset);
}
return bytes;
}
/**
* bytes[7] = value >> 56
* bytes[6] = value >> 48
* bytes[5] = value >> 40
* bytes[4] = value >> 32
* bytes[3] = value >> 24
* bytes[2] = value >> 16
* bytes[1] = value >> 8
* bytes[0] = value >> 0
* @param value
* @return
*/
public static byte[] longToBytes(long value){
int length = 8;
byte[] bytes = new byte[length];
for (int i = length - 1; i >= 0; i--) {
int offset = i * 8; //56, 48, 40, 32, 24, 16, 8
bytes[i] = (byte) (value >> offset);
}
return bytes;
}
/**
* 操作符 << 的優(yōu)先級(jí)比 & 高
* intValue = (bytes[3] & 0xFF) << 24
| (bytes[2] & 0xFF) << 16
| (bytes[1] & 0xFF) << 8
| (bytes[0] & 0xFF) << 0
* @param bytes
* @return
*/
public static int bytesToInt (byte[] bytes){
int length = 4;
int intValue = 0;
for (int i = length - 1; i >= 0; i--) {
int offset = i * 8; //24, 16, 8
intValue |= (bytes[i] & 0xFF) << offset;
}
return intValue;
}
/**
* 操作符 << 的優(yōu)先級(jí)比 & 高
* longValue = (long)(bytes[7] & 0xFF) << 56
| (long)(bytes[6] & 0xFF) << 48
| (long)(bytes[5] & 0xFF) << 40
| (long)(bytes[4] & 0xFF) << 32
| (long)(bytes[3] & 0xFF) << 24
| (long)(bytes[2] & 0xFF) << 16
| (long)(bytes[1] & 0xFF) << 8
| (long)(bytes[0] & 0xFF) << 0
* @param bytes
* @return
*/
public static long bytesToLong (byte[] bytes){
int length = 8;
long longValue = 0;
for (int i = length - 1; i >= 0; i--) {
int offset = i * 8; //56, 48, 40, 32, 24, 16, 8
longValue |= (long)(bytes[i] & 0xFF) << offset; //一定要先強(qiáng)制轉(zhuǎn)換成long型再移位, 因?yàn)?xFF為int型
}
return longValue;
}
public static float bytesToFloat(byte[] bytes) {
return Float.intBitsToFloat(bytesToInt(bytes));
}
public static double bytesToDouble(byte[] bytes) {
return Double.longBitsToDouble(bytesToLong(bytes));
}
public static byte[] floatToBytes(float value){
return intToBytes(Float.floatToIntBits(value));
}
public static byte[] doubleToBytes(double value){
return longToBytes(Double.doubleToLongBits(value));
}
}
====================================================================================
public static String bytes2HexString(byte[] b) {
String ret = "";
for (int i = 0; i < b.length; i++) {
String hex = Integer.toHexString(b[ i ] 0xFF);
if (hex.length() == 1) {
hex = '0' + hex;
}
ret += hex.toUpperCase();
}
return ret;
}
上面是將byte[]轉(zhuǎn)化十六進(jìn)制的字符串,注意這里b[ i ] & 0xFF將一個(gè)byte和 0xFF進(jìn)行了與運(yùn)算,
然后使用Integer.toHexString取得了十六進(jìn)制字符串,可以看出 b[ i ] & 0xFF運(yùn)算后得出的仍然
是個(gè)int,那么為何要和 0xFF進(jìn)行與運(yùn)算呢?
直接 Integer.toHexString(b[ i ]);,將byte強(qiáng)轉(zhuǎn)為int不行嗎?答案是不行的.
其原因在于:
1.byte的大小為8bits而int的大小為32bits
2.java的二進(jìn)制采用的是補(bǔ)碼形式
在這里先溫習(xí)下計(jì)算機(jī)基礎(chǔ)理論 byte是一個(gè)字節(jié)保存的,有8個(gè)位,即8個(gè)0、1。
8位的第一個(gè)位是符號(hào)位, 也就是說(shuō)
0000 0001代表的是數(shù)字1
1000 0000代表的就是-1
所以
正數(shù)最大為0111 1111,也就是數(shù)字127
負(fù)數(shù)最大為1111 1111,也就是數(shù)字-128
上面說(shuō)的是二進(jìn)制原碼,
但是在java中采用的是補(bǔ)碼的形式,而不是原碼,
下面介紹下什么是補(bǔ)碼
1、反碼:一個(gè)數(shù)如果是正,則它的反碼與原碼相同; 一個(gè)數(shù)如果是負(fù),則符號(hào)位為1,其余各位是對(duì)原碼取反;
2、補(bǔ)碼:利用溢出,我們可以將減法變成加法,
對(duì)于十進(jìn)制數(shù),從9得到5可用減法: 9-4=5。
因?yàn)?+6=10,我們可以將6作為4的補(bǔ)數(shù),改寫(xiě)為加法: 9+6=15(去掉高位1,也就是減10)得到5.
對(duì)于十六進(jìn)制數(shù),從c到5可用減法: c-7=5。
因?yàn)?+9=16 將9作為7的補(bǔ)數(shù),改寫(xiě)為加法: c+9=15(去掉高位1,也就是減16)得到5.
在計(jì)算機(jī)中,如果我們用1個(gè)字節(jié)表示一個(gè)數(shù),一個(gè)字節(jié)有8位,超過(guò)8位就進(jìn)1,在內(nèi)存中情況為(1 0000 0000),進(jìn)位1被丟棄。
所以補(bǔ)碼是這樣運(yùn)算的:
⑴一個(gè)數(shù)為正,則它的原碼、反碼、補(bǔ)碼相同
⑵一個(gè)數(shù)為負(fù),高符號(hào)位為1,其余各位是對(duì)原碼取反,然后整個(gè)數(shù)加1,(符號(hào)位不變,其余為取反再加1)
-1的原碼為 10000001
-1的反碼為 11111110 再 + 1
-1的補(bǔ)碼為 11111111
0的原碼為 00000000
0的反碼為 11111111 (正零和負(fù)零的反碼相同) 再 + 1
0的補(bǔ)碼為 100000000(舍掉打頭的1,正零和負(fù)零的補(bǔ)碼相同)
Integer.toHexString的參數(shù)是int,如果不進(jìn)行&0xff,那么當(dāng)一個(gè)byte轉(zhuǎn)換成int時(shí),
由于int是32位,而byte只有8位,這時(shí)會(huì)進(jìn)行補(bǔ)位,補(bǔ)位補(bǔ)的是1
例如:補(bǔ)碼11111111的十進(jìn)制數(shù)為-1,轉(zhuǎn)換為int時(shí)變?yōu)?1111111 11111111 11111111 11111111
好多1啊,呵呵!即 0xff ff ff ff ,但是這個(gè)數(shù)是不對(duì)的,
這種補(bǔ)位就會(huì)造成誤差。和0xff相與后,高24bits就會(huì)被清0了,結(jié)果就對(duì)了。(任是補(bǔ)碼11111111)