轉自:http://www.cnblogs.com/junsky/archive/2009/08/06/1540727.html
今天在看base64編碼轉換時,既然對負數的二進制表示有些遺忘,在網上找了一下資料,貼出來已備在此遺忘:
假設有一個 int 類型的數,值為5,那么,我們知道它在計算機中表示為:
00000000 00000000 00000000 00000101
5轉換成二制是101,不過int類型的數占用4字節(32位),所以前面填了一堆0。
現在想知道,-5在計算機中如何表示?
在計算機中,負數以其正值的補碼形式表達。
什么叫補碼呢?這得從原碼,反碼說起。
原碼:一個整數,按照絕對值大小轉換成的二進制數,稱為原碼。
比如 00000000 00000000 00000000 00000101 是 5的 原碼。
反碼:將二進制數按位取反,所得的新二進制數稱為原二進制數的反碼。
取反操作指:原為1,得0;原為0,得1。(1變0; 0變1)
比如:將00000000 00000000 00000000 00000101每一位取反,得11111111 11111111 11111111 11111010。
稱:11111111 11111111 11111111 11111010 是 00000000 00000000 00000000 00000101 的反碼。
反碼是相互的,所以也可稱:
11111111 11111111 11111111 11111010 和 00000000 00000000 00000000 00000101 互為反碼。
補碼:反碼加1稱為補碼。
也就是說,要得到一個數的補碼,先得到反碼,然后將反碼加上1,所得數稱為補碼。
比如:00000000 00000000 00000000 00000101 的反碼是:11111111 11111111 11111111 11111010。
那么,補碼為:
11111111 11111111 11111111 11111010 + 1 = 11111111 11111111 11111111 11111011
所以,-5 在計算機中表達為:11111111 11111111 11111111 11111011。轉換為十六進制:0xFFFFFFFB。
再舉一例,我們來看整數-1在計算機中如何表示。
假設這也是一個int類型,那么:
1、先取1的原碼:00000000 00000000 00000000 00000001
2、得反碼: 11111111 11111111 11111111 11111110
3、得補碼: 11111111 11111111 11111111 11111111
可見,-1在計算機里用二進制表達就是全1。16進制為:0xFFFFFF
//==============================================================
//以下是Based64轉碼,在http傳輸的時候用得比較多。

1 /** 2 This stream filter converts a stream of bytes to their 3 Base64 encoding. 4 5 Base64 encoding encodes 3 bytes into 4 characters. 6 |11111122|22223333|33444444| 7 Each set of 6 bits is encoded according to the 8 toBase64 map. If the number of input bytes is not 9 a multiple of 3, then the last group of 4 characters 10 is padded with one or two = signs. Each output line 11 is at most 76 characters. 12 */ 13 class Base64OutputStream extends FilterOutputStream 14 { 15 /** 16 Constructs the stream filter 17 @param out the stream to filter 18 */ 19 public Base64OutputStream(OutputStream out) 20 { 21 super(out); 22 } 23 24 public void write(int c) throws IOException 25 { 26 inbuf[i] = c; 27 i++; 28 if (i == 3) 29 { 30 super.write(toBase64[(inbuf[0] & 0xFC) >> 2]); 31 super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]); 32 super.write(toBase64[((inbuf[1] & 0x0F) << 2) | ((inbuf[2] & 0xC0) >> 6)]); 33 super.write(toBase64[inbuf[2] & 0x3F]); 34 col += 4; 35 i = 0; 36 if (col >= 76) 37 { 38 super.write('\n'); 39 col = 0; 40 } 41 } 42 } 43 44 public void flush() throws IOException 45 { 46 if (i == 1) 47 { 48 super.write(toBase64[(inbuf[0] & 0xFC) >> 2]); 49 super.write(toBase64[(inbuf[0] & 0x03) << 4]); 50 super.write('='); 51 super.write('='); 52 } 53 else if (i == 2) 54 { 55 super.write(toBase64[(inbuf[0] & 0xFC) >> 2]); 56 super.write(toBase64[((inbuf[0] & 0x03) << 4) | ((inbuf[1] & 0xF0) >> 4)]); 57 super.write(toBase64[(inbuf[1] & 0x0F) << 2]); 58 super.write('='); 59 } 60 } 61 62 private static char[] toBase64 = 63 { 64 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 65 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'a', 'b', 'c', 'd', 'e', 'f', 66 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 67 'w', 'x', 'y', 'z', '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', '+', '/' 68 }; 69 70 private int col = 0; 71 private int i = 0; 72 private int[] inbuf = new int[3]; 73 }