Java-defined base64 encoding conversion, javabase64
1. base64 encoding and conversion
The so-called base64 encoding means converting characters to the character set "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789 +/" according to the rules. The specific rules are as follows:
A. divide each 3 bytes into a group of 24 bits in total. Each 6-bit group adds 00 to the front of each group and changes to 32bit. In this way, the three bytes are extended into four sections and encoded according to the above character set.
B. If the number of bytes is less than 3:
1) when the number of bytes is 2, there are 16 bits in total. Each 6-bit group has only 4 bits in the last group. In addition to adding 00 in the front, the last group also needs to be supplemented with 00, which is encoded according to the above Character Set and supplemented with = at the end.
2) 8 bits in total when the number of bytes is 1. Each 6-bit group, the last group only has 2 bits. If the first group is added to the group with 00, the Group must be supplemented with 0000, Which is encoded according to the above Character Set and supplemented with = at the end ..
2. How does a computer represent a negative number. (Take the basic byte type as an example)
In java, a byte is an 8-bit byte in total, which can be expressed in the range of bytes --11111111 (0 ~ 255 ). In the computer, the 0--01111111 is represented as 0 ~ 127, expressed as-128 ~ -1. Then, for example,-127 + 127, that is, 01111111 + 10000001 = 100000000, and the sum is equal to the modulo (256), that is, complement each other.
3. java bit operation.
In java (addition, subtraction, multiplication, division, right shift, left shift, unsigned right shift, bitwise AND, bitwise OR, bitwise exclusive or) operations, the byte is first executed, short and char are converted to int before corresponding operation. Example:
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, forcibly converted to byte. In this case, overflow occurs. java processing overflow (+-) 256 * n, if 256 is a modulo of the byte type, the result is-129 + 256 = 127; byte s5 =-28; System. out. println (s5 <2); // The result is-112. It is first converted to the int type, and 0 is added on the right side. The System is discarded from the high position. out. println (s5> 2); // The result is-7. It is first converted to the int type, and the System is discarded at the low position. out. println (s5 >>> 2); // The result is 1073741817. It is first converted to the int type, and 0 is added for the high position. The System is discarded for the low position. out. println (s5 & 0xFC)> 2 );}}
4. Implement base64 encoding in java
/*** @ Author zyw February 21, 2017 */package test; import java. io. unsupportedEncodingException;/*** 1. complement 2. bitwise operation 3. base64 ** @ description: the first step of learning to encrypt base64 is to use every three bytes as a group with a total of 24 binary digits. * Step 2: divide the 24 binary bits into four groups. Each group has 6 binary bits. Step 3: Add two 00 in front of each group and extend it to 32 binary bits, that is, four bytes. **/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 ("China fggfgfgf234234 % # $ % ^ # $", "UTF-8 ")); // 5Lit5Zu9ZmdnZmdmZ2YyMzQyMzQlIyQlXiMkJA ==}/ *** base64 encryption * @ 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 (); // encrypt int lengthDataBits = text. length * 8; int fewerThan24bits = lengthDataBits % 24; // whether the length of the encrypted string exceeds 24int NumberTriplets = lengthDataBits/24; int number = fewerThan24bits! = 0? NumberTriplets + 1: numberTriplets; // calculates the total number of characters after string encryption. char [] toBase64Text = new char [number * 4]; // stores the results of byte s1, s2, and 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]; // The first six-digit toBase64Text [order ++] = base [(s1 & 0x03) <4) + (s2 & 0xF0)> 4)]; // The second six-digit toBase64Text [order ++] = Base [(s2 & 0x0F) <2) + (s3 & 0xC0)> 6)]; // The third 6-bit toBase64Text [order ++] = base [s3 & 0x3f]; // The fourth 6-bit}/*** one-byte condition: add the first two zeros to the last group of the eight binary bits of this byte, and then add four zeros. In this way, a two-digit Base64 encoding is obtained, and * adds two "=" signs at the end. */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;}/*** two bytes: convert a total of 16 binary bits of the two bytes into three groups, in addition to adding two zeros to the first group, the last group must be followed by two zeros. * In this way, a three-digit Base64 encoding is obtained, and a "=" sign is added at the end. */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 );}}