所謂base64編碼,即按照規則把字符轉化為"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/"這個字符集中的字符。具體規則如下:
a.把每3個字節為一組,共24bit。每6bit一小組,每組前面加00,變為32bit。這樣3個字節被擴展成了4個節,按照上面字符集編碼。
b.如果字節數不足3:
1)當字節數為2時,共16bit。每6bit一小組,最后一組即只有4bit,則不止前面加00以外,后面也要補00,按照上面字符集編碼,末尾補=。
2)當字節數為1時,共8bit。每6bit一小組,最后一組即只有2bit,則不止前面加00以外,后面也要補0000,按照上面字符集編碼,末尾補==.。
2.計算機如何表示負數。(以byte基本類型為例)
在java中一個byte為一個字節共8bit,可表示范圍00000000——11111111(0~255)。在計算機中把0——01111111表示為0~127,把10000000——11111111表示為-128 ~-1。那么這樣如-127+127,即01111111+10000001=100000000,相加等于模(256),即互為補碼。
3.java位運算。
在java中(加,減,乘,除,右移,左移,無符號右移,位與,位或,位異或)操作,均會是首先將byte,short,char轉化為int,再進行相應運算。舉例:
1
2
3
4
5
6
7
8
9
10
11
12
|
public class Test { public static void main(String[] args) { byte s1 = ( byte ) 0xFF ; // -1 byte s2 = ( byte ) 0x80 ; // -128 System.out.println(( byte )(s1+s2)); //s1+s2=-129,強制轉化為byte,此時溢出,java處理溢出(+-)256*n,256為byte類型的模,則結果為-129+256=127; byte s5 = - 28 ; System.out.println(s5 << 2 ); // 結果為-112, 先轉換為int類型,右邊補0,高位舍棄 System.out.println(s5>> 2 ); //結果為-7,先轉換為int類型,高位補符號位,低位舍棄 System.out.println(s5>>> 2 ); //結果為1073741817,先轉換為int類型,高位補0,低位舍棄 System.out.println((s5& 0xFC )>> 2 ); } } |
4.java實現base64編碼方式
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
/** * @author zyw 2017年2月21日 */ package test; import java.io.UnsupportedEncodingException; /** * 1.補碼 2.位運算 3.base64 * * @description 學習base64加密 第一步,將每三個字節作為一組,一共是24個二進制位。 * 第二步,將這24個二進制位分為四組,每個組有6個二進制位。 第三步,在每組前面加兩個00,擴展成32個二進制位,即四個字節。 * */ public class Base64 { static private final int SIXTEENBIT = 16 ; static private final int EIGHTBIT = 8 ; static private final char PAD = '=' ; public static void main(String[] args) throws UnsupportedEncodingException { System.out.println(Base64.toBase64( "中國fggfgfgf234234%#$%^#$$" , "UTF-8" )); //5Lit5Zu9ZmdnZmdmZ2YyMzQyMzQlIyQlXiMkJA== } /** * base64加密 * @param str * @param charsetName * @return * @throws UnsupportedEncodingException */ public static String toBase64(String str, String charsetName) throws UnsupportedEncodingException { if (str.length() < 0 ) return "" ; byte [] text = str.getBytes(charsetName); char [] base = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" .toCharArray(); // 加密 int lengthDataBits = text.length * 8 ; int fewerThan24bits = lengthDataBits % 24 ; // 加密字符串長度是否超過24 int numberTriplets = lengthDataBits / 24 ; int number = fewerThan24bits != 0 ? numberTriplets + 1 : numberTriplets; // 計算字符串加密后字符總個數 char [] toBase64Text = new char [number * 4 ]; // 用來保存結果 byte s1, s2, s3; int index = 0 , order = 0 ; for ( int i = 0 ; i < numberTriplets; i++) { s1 = text[index++]; s2 = text[index++]; s3 = text[index++]; toBase64Text[order++] = base[(s1 & 0xFC ) >> 2 ]; // 第一個6位 toBase64Text[order++] = base[((s1 & 0x03 ) << 4 ) + ((s2 & 0xF0 ) >> 4 )]; // 第二個6位 toBase64Text[order++] = base[((s2 & 0x0F ) << 2 ) + ((s3 & 0xC0 ) >> 6 )]; // 第三個6位 toBase64Text[order++] = base[s3 & 0x3f ]; // 第四個6位 } /** * 一個字節的情況:將這一個字節的8個二進制位最后一組除了前面加二個0以外,后面再加4個0。這樣得到一個二位的Base64編碼, * 再在末尾補上兩個"="號。 */ if (fewerThan24bits == EIGHTBIT) { byte last = text[index++]; toBase64Text[order++] = base[(last & 0xFC ) >> 2 ]; toBase64Text[order++] = base[((last & 0x03 ) << 4 )]; toBase64Text[order++] = PAD; toBase64Text[order++] = PAD; } /** * 二個字節的情況:將這二個字節的一共16個二進制位,轉成三組,最后一組除了前面加兩個0以外,后面也要加兩個0。 * 這樣得到一個三位的Base64編碼,再在末尾補上一個"="號。 */ if (fewerThan24bits == SIXTEENBIT) { s1 = text[index++]; s2 = text[index++]; toBase64Text[order++] = base[(s1 & 0xFC ) >> 2 ]; toBase64Text[order++] = base[(s1 & 0x03 ) << 4 + ((s2 & 0xF0 ) >> 4 )]; toBase64Text[order++] = base[(s2 & 0x0f ) << 2 ]; toBase64Text[order++] = PAD; } return new String(toBase64Text); } } |
以上就是本文的全部內容,希望本文的內容對大家的學習或者工作能帶來一定的幫助,同時也希望多多支持服務器之家!
原文鏈接:http://www.cnblogs.com/yunwuzhan/p/6431184.html