HEX (十六進位) EA EA
Oct(十進位) 有符號 無符號
-22 234
Bin (二進位) 1110 1010 11101010
備忘:有符號數
正數 源碼,反碼,補碼都一致
負數 詳解見下,例如 :11101010
| |
1 |
1 |
1 |
0 |
1 |
0 |
1 |
0 |
|
| |
符號位(不參與取反與+1行為) |
|
|
|
|
|
|
|
|
| 1)取反 |
1 |
0 |
0 |
1 |
0 |
1 |
0 |
1 |
得到反碼 |
| 2)+1 |
1 |
1 |
1 |
0 |
1 |
0 |
1 |
0 |
得到補碼 |
| |
1(代表負數) |
|
|
|
|
|
|
|
-22 |
Hex String --> Byte
//1 Hex String --> Integer//2 Integer --> Bytepublic Byte convertFromHexString2Byte(String hexStr){Integer i = Integer.parseInt(hexStr,16);Byte b = i.byteValue();}
Byte -->Hex String
//1 Byte -- >Integer//2 Integer --> String public static String fromByte2HexString(Byte b){ Integer i = b.intValue(); String s = Integer.toHexString(i); return s;}
一個byte佔用8位,一個HEX字元用4位表示 ===>1個byte 代表 2個HEX字元===>
[] [] [] [] [] [] [] []
高4位 低4位
H L
- byte類型取值範圍(byte 佔8位,有符號 -128 --- 127 )
推理
| |
|
|
|
|
|
|
|
|
含義 |
|
| 最大正數 |
0 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
==>127 |
20 +21+….+28-1 =28 -1 =127 |
| 最大負數 |
1 |
0 |
0 |
0 |
0 |
0 |
0 |
0 |
===>128 |
-27 |
| |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
反碼 |
|
| 1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
1 |
補碼 |
|
| |
|
|
|
|
|
|
|
|
|
|
取值範圍
| byte |
1 位元組 |
8bit |
-2(7)---2(8)-1 |
有符號 -2n-1 --- 2n-1 -1 |
| short |
2位元組 |
16bit |
-215 -215-1 |
同上 |
| int |
4位元組 |
32bit |
|
同上 |
| long |
8位元組 |
64bit |
|
同上 |
| char |
2位元組 |
16bit |
0--216-1 |
無符號 0—2N-1 |
public static String byte2HexString(byte[] b){ String ref = ""; for(int i=0; i<b.length;i++){ /*
Integer.toHexString(b[i]&0xFF);
作用是 int(十進位 有符號) ===> String(十六進位)
b[i]&0xFF 作用是 將byte轉換為int類型
byte 8 位 int 32位 byte轉int 位元不夠需要補位 補位有偏差故需要清零高24位 即&0xFF*/
String hex = Integer.toHexString(b[i]&0xFF); if(hex.length()==1){ hex='0'+hex; } ref+=hex.toUpperCase(); } return ref;}/*FF 十六進位1111 1111 二進位-1 有符號 十進位255 無符號*/
兩邊都成立才可以為真
1&1 --》1
1&0 --》0
0&1--》0
0&0 --》0
清零 ==》 &0
置為1 == 》&1
byte b = 24;
int i = b&0xFF; //清零高24位