Character encoding and decoding

Source: Internet
Author: User
Tags binary to decimal truncated

Today, the boss gave the segment encryption and decryption code to share with him:

* String (encoding/encryption) and (Decoding/Decryption ).
*
* Rules:
*
* In base64, the code table is composed of [A-Z, A-Z, 0-9, +,/, = (PAD. Here, the code table consists of [a-H, J-R, T-Z, 2-9:
* A B c d e f g h I j k l m n o p q r
* 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
* S t u v w x y z 2 3 4 5 6 7
* 18 19 20 21 22 23 24 25 26 28 29 30 31
*------------------------------------------------

* In base64, the binary is connected into a string, and then divided by 6 digits. After the split, add 0 in front. everyone on this earth knows that it is not much to say.
* Here, the split step is slightly changed. The split is based on five digits. If the split is enough, it will be fine. If not, what should we do?
*
* "=" Is used in base64. Here, we add 0 in the front and then add zero in the back.

For example, after the string "AAA" (encoding/encryption) is "mfqwc"

* Binary: 01100001 01100001 01100001 after conversion: (000) 01100 (000) 00101 (000) 10000
* (000) 10110 (000) 0001 (0) decimal: 12 5 16 22 2 code table correspondence: M f Q W C

* Code table: M f q w c decimal: 12 5 16 22 2 binary: 00001100 00000101 00010000 00010110
* 00000010 before and after 0: 01100 00101 10000 10110 after merge: 00010

* Then, divide the length of the merged string by 8 and find that there is an extra 0:
*
* Binary: 01100001 01100001 01100001 0
*
* If it is more, forget it. (In fact, it is 0 after the remainder of {encoding/encryption ). Then, convert byte [] Back to the string. OK! See "AAA" again.
*
* A little worth noting, UTF-8, GBK, gb18030 is generally no problem, but gb2312 may not be rich enough character set, traditional Chinese characters in decode when into a question mark.

Code:

All have Annotations:

Public class codec {

/**
* Code table
*/
Private Final Static string codec_table = "abcdefghjklmnopqrtuvwxyz23456789 ";

/**
* 5-bit bytes
*/
Public final static int five_bit = 5;

/**
* 8-bit bytes
*/
Public final static int eight_bit = 8;

/**
* Binary
*/
Public final static int binary = 2;

/**
* (Encoding/encryption) string, using the character set in the default language environment.
*
* @ Param keys
* String that requires (encoding/encryption)
*
* @ Return (encoded/encrypted) String
*/

Public static string encode (string keys ){

Return encode (keys, null );

}

/**
* (Encoding/encryption) String
*
* @ Param keys
* String that requires (encoding/encryption)
* @ Param characterset
* Character Set
*
* @ Return (encoded/encrypted) String
*/

Public static string encode (string keys, string characterset ){

If (Keys = NULL | "". Equals (KEYS )){
Return "";
}

Byte [] keybytes = NULL;

If (characterset = NULL | characterset. Length () <1 ){

// Use the character set in the default language environment.
Keybytes = keys. getbytes ();

} Else {

Try {

// Use the specified character set.
Keybytes = keys. getbytes (characterset );

} Catch (unsupportedencodingexception e ){
// Ignore...
}

}
Return encode (keybytes );
}

/**
* (Encoding/encryption) byte array
*
* @ Param keybytes
* Bytes array (encoding/encryption) required
*
* @ Return (encoded/encrypted) String
*/
Private Static string encode (byte [] keybytes ){

If (keybytes = NULL | keybytes. Length <1 ){

Return "";

}

/*
* Merge binary codes, for example, 00101010 11010011 00101101 10100011
* 00101010110100110010110110100011
*/

Stringbuilder mergrd = new stringbuilder ();

For (INT I = 0; I <keybytes. length; I ++ ){

Formatutil. formatbinary (keybytes [I], mergrd );

}

/*
* Calculates the number of groups that can be divided by five bits, for example, 00101010110100110010110110100011 to 00101 01011.
* 01001 10010 11011 01000 11 | (this 11 represents the remaining digits)
*/

Int groupcount = mergrd. Length ()/five_bit;

// Calculate the remaining digits
Int lastcount = mergrd. Length () % five_bit;

// Similar to the data paging algorithm, add 1 if there is an remainder.
If (lastcount> 0 ){

Groupcount + = 1;

}

/*
* (Encoding/encryption)
*/

Stringbuilder sbencoded = new stringbuilder ();

// Conditions required for Loop
Int formax = groupcount * five_bit;

// Increment by 5 digits each time.
For (INT I = 0; I <formax; I + = five_bit ){

// End point
Int end = I + five_bit;

/*
* If the end point is longer than the merged binary code string, it is equivalent to a remainder and indicates that the current loop has reached (the merged binary code string length % ).
* Five_bit.
*/

// Mark whether it is the remainder
Boolean flag = false;

If (end> mergrd. Length ()){

/*
* If the end point is longer than the merged binary code string, the end point must be reset to the length of the merged binary code string, which is equivalent to (I +
* Lastcount). and reset the tag.
*/

End = (I + lastcount );

Flag = true;

}

// Capture
String strfivebit = mergrd. substring (I, end );

// Convert the image from binary to decimal.
Int intfivebit = integer. parseint (strfivebit, binary );

If (FLAG ){

/*
* If the end point is longer than the merged binary string length, or the end point reaches the remainder, the left-shift operation is required. Assume that the remaining binary digits are: 11,
* Then we need to add 0 from the end, and the value after the Left shift operation is (000) 11 (000)
*/

Intfivebit <= (five_bit-lastcount );

}

// Use this decimal number as the index of the code table to obtain the corresponding character and append it to sbencoded
Sbencoded. append (codec_table.charat (intfivebit ));

}

Return sbencoded. tostring ();

}

/**
* (Decoding/Decryption) string, using the character set in the default language environment.
*
* @ Param code
* String to be decoded/decrypted
*
* @ Return (decoded/decrypted) String
*/
Public static string decode (string code ){

Return decode (Code, null );

}

/**
* (Decoding/Decryption) String
*
* @ Param code
* String to be decoded/decrypted
* @ Param characterset
* Character Set
*
* @ Return (decoded/decrypted) String
*/
Public static string decode (string code, string characterset ){

If (code = NULL | code. Length () <1 ){

Return "";

}

/*
* Remove each character and obtain the corresponding index from the code table.
*/

Stringbuilder sbbinarys = new stringbuilder ();

For (INT I = 0; I <code. Length (); I ++ ){

// Obtain the corresponding index from the code table
Int Index = getcodectableindex (code. charat (I ));

// Convert the decimal index into a binary string
String indexbinary = integer. tobinarystring (INDEX );

// Remove the first three zeros and append them to sbbinarys.
Formatutil. formatbinary (indexbinary, sbbinarys, five_bit );

}

/*
* Split by 8 bits and discard the remainder. When the remainder is separated (encoding/encryption), the remaining remainder is supplemented by 0.
*/

Byte [] binarys = new byte [sbbinarys. Length ()/eight_bit];

For (INT I = 0, j = 0; I <binarys. length; I ++ ){

// Take one copy of each 8 bits
String sub = sbbinarys. substring (J, J + = eight_bit );

// Convert the truncated binary string to decimal
Integer intbinary = integer. valueof (sub, binary );

Binarys [I] = intbinary. bytevalue ();

}

String decoded = NULL;

If (characterset = NULL | characterset. Length () <1 ){

// Use the character set in the default language environment.
Decoded = new string (binarys );

} Else {

Try {

// Use the specified character set.
Return new string (binarys, characterset );

} Catch (unsupportedencodingexception e ){
// Ignore...
}
}
Return decoded;
}

/**
* Traverses codec_table Based on the given characters and returns the corresponding subscript. If not found,-1 is returned.
*
* @ Param code
* Characters in the codec_table range.
*
* @ Return indicates the subscript corresponding to the codec_table character. If no subscript is found,-1 is returned.
*/
Private Static int getcodectableindex (char code ){

For (INT I = 0; I <codec_table.length (); I ++ ){

If (codec_table.charat (I) = Code ){

Return I;

}

}

Return-1;

}

}

Format binary:

Package cn.edu. WTU. Code;
/**
* Formatting Tool
* <PRE>
* Class Name: formatutil
* Modifications:
* Modifier Wei rongting; 2011-10-8; create new class formatutil.
* </PRE>
*/
Public class formatutil {

/**
* Format the binary. The default value is 8 bits. If the value is exceeded, the value is truncated. If the value is insufficient, the value is set to zero.
* Format: "00000000", similar to numberformat pattern.
*
* @ Author gembler
* @ Version 03:15:06
*
* @ Param binary
* Bytes to be formatted.
*
* @ Return refers to the formatted string.
*/
Public static string formatbinary (byte binary ){

Return formatbinary (binary, null). tostring ();

}

/**
* Format the binary. If the value is exceeded, it is truncated. If the value is insufficient, it is set to zero. Format: "00000000", similar to the pattern of numberformat.
*
* @ Author gembler
* @ Version 03:15:09
*
* @ Param binary
* Bytes to be formatted.
* @ Param bitcount
* The number of digits to be formatted.
*
* @ Return refers to the formatted string.
*/
Public static string formatbinary (byte binary, int bitcount ){

Return formatbinary (binary, null, bitcount). tostring ();

}

/**
* Format the binary. If the value is exceeded, it is truncated. If the value is insufficient, it is set to zero. Format: "00000000", similar to the pattern of numberformat.
*
* @ Author gembler
* @ Version 03:15:12
*
* @ Param binary
* Bytes to be formatted.
* @ Param toappendto
* The builder to which the object is appended.
*
* @ Return the formatted stringbuilder.
*/
Public static stringbuilder formatbinary (byte binary,
Stringbuilder toappendto ){

Return formatbinary (binary, toappendto, codec. eight_bit );
}

/**
* Format the binary. If the value is exceeded, it is truncated. If the value is insufficient, it is set to zero. Format: "00000000", similar to the pattern of numberformat.
*
* @ Author gembler
* @ Version 03:15:16
*
* @ Param binary
* Bytes to be formatted.
* @ Param toappendto
* The builder to which the object is appended.
* @ Param bitcount
* The number of digits to be formatted.
*
* @ Return the formatted stringbuilder.
*/
Public static stringbuilder formatbinary (byte binary,
Stringbuilder toappendto, int bitcount ){

String strbinary = integer. tobinarystring (Binary );

Return formatbinary (strbinary, toappendto, bitcount );
}

/**
* Format the binary. If the value is exceeded, it is truncated. If the value is insufficient, it is set to zero. Format: "00000000", similar to the pattern of numberformat.
*
* @ Author gembler
* @ Version 03:15:20
*
* @ Param binary
* Bytes to be formatted.
*
* @ Return refers to the formatted string.
*/
Public static string formatbinary (string binary ){

Return formatbinary (binary, null). tostring ();

}

/**
* Format the binary. If the value is exceeded, it is truncated. If the value is insufficient, it is set to zero. Format: "00000000", similar to the pattern of numberformat.
*
* @ Author gembler
* @ Version 03:15:24
*
* @ Param binary
* Bytes to be formatted.
* @ Param bitcount
* The number of digits to be formatted.
*
* @ Return refers to the formatted string.
*/
Public static string formatbinary (string binary, int bitcount ){

Return formatbinary (binary, null, bitcount). tostring ();

}

/**
* Format the binary. If the value is exceeded, it is truncated. If the value is insufficient, it is set to zero. Format: "00000000", similar to the pattern of numberformat.
*
* @ Author gembler
* @ Version 03:15:27
*
* @ Param binary
* Bytes to be formatted.
* @ Param toappendto
* The builder to which the object is appended.
*
* @ Return the formatted stringbuilder.
*/
Public static stringbuilder formatbinary (string binary,
Stringbuilder toappendto ){

Return formatbinary (binary, toappendto, codec. eight_bit );

}

/**
* Format the binary. If the value is exceeded, it is truncated. If the value is insufficient, it is set to zero. Format: "00000000", similar to the pattern of numberformat.
*
* @ Author gembler
* @ Version 03:15:31
*
* @ Param binary
* Bytes to be formatted.
* @ Param toappendto
* The builder to which the object is appended.
* @ Param bitcount
* The builder to which the object is appended.
*
* @ Return the formatted stringbuilder.
*/
Public static stringbuilder formatbinary (string binary,
Stringbuilder toappendto, int bitcount ){

If (Binary = NULL | binary. Length () <1 ){

Return toappendto;

}

If (toappendto = NULL ){

Toappendto = new stringbuilder ();

}

If (Binary. Length () = bitcount ){

Toappendto. append (Binary );

Return toappendto;

}

/*
* Fill in 0 before, for example, "100011" to "00100011"
*/
If (Binary. Length () <bitcount ){

Stringbuilder formatted = new stringbuilder ();

Formatted. append (Binary );

Do {

Formatted. insert (0, "0 ");

} While (formatted. Length () <bitcount );

Toappendto. append (formatted );

Return toappendto;

}

/*
* Truncate, such as: "11111111111111111111111110100011" to "10100011"
*/
If (Binary. Length ()> bitcount ){

String intercepted = binary. substring (Binary. Length ()-bitcount );

Toappendto. append (intercepted );

Return toappendto;

}

Return toappendto;
}

}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.