0, write in front
The primary purpose of the encoding discussed here is to convert the non-display binary array to a displayable string, including its inverse operation. Similar methods are used through specific protocols or encrypted decryption.
More than Base64 in this type of operation is Base64, such as MIME, DOTNET provides Base64.
ENCODE and DECODE methods, quite convenient. However, Base64 is usually composed of "AZ", "AZ", 0-9, " " and "=", which contains a lot of confusing characters, such as "1", "I", "L", "0 "And" o "or" 2 "and" z "seem to be uncomfortable. In particular, when it is encoded as a serial number, it should not be confused letters, so there is another coding form called Base24, and the brothers who have used MS products must be very familiar. However, Base24 has more than one bend, first put it first, let's speak Base32, can basically meet the requirements, and very intuitive encoding methods.
1, coding principle
The principle of Base32 is exactly the same as Base64, so let's take a look at how the base64 coding is.
Base64 As the name refers to indicate all ASC characters with 64 display characters, 64 is 6BITS, and ASC characters have a total of 256, that is, 8bits, very simple, take a minimum number of conventions, 24, words, 3 ASC characters are represented by 4 Base64 characters. That is, when encoding, 3 sets of ASC characters generate 4 base64 characters, decoding 4 groups, restoring 3 ASC characters. The string after Base64 encoding according to this principle should increase the length of 1/3.
Here, the so-called code is taken at a time, the value is converted as an index number, using this index number, and take the corresponding character array to the previously defined length of 64, take the corresponding character, the decoding is the counterput, according to the character. Predefined the index value in the array, then press a set of 8bits to restore the ASC characters.
Compared with only one difference than one difference, Base32 and Base64 is to represent 256 ASC characters with 32 characters, that is, a group of 5 ASC characters can generate 8 BASE characters, and vice versa.
2, source code
For code to achieve such functions, there is no difficulty, if it is not considered, as long as you care, it will not be wrong. Some people have written it once, so I will share it with everyone.
The following category also provides a feature that you can customize the encoded string, pay attention to this attribute of Base32map.
/// ********************************************************* **************************************** ///// Module: CBase32 /// Author : Mittermeyer /// Comment: /// /// History: /// 2004-08-31 mittermeyer create ////// Copyright 1995-2004 by mittermeyer.all Rights reserved.// ***** *********************************************************** ***********************
Using system; using system.text;
namespace Mittermeyer {///
public static String Base32Map {get {return m_acBase32Map.ToString ();} set {if (! value = null && value.Length> = Base32MapLength) {m_acBase32Map = value.ToCharArray ();} else {m_acBase32Map = DefaultBase32Map.ToCharArray () }}}
Public Static Char getcharacter (int32 dwindex) {char cmappingdata = '/ 0';
IF (m_acbase32map! = null && dwindex> = 0 && dwindex Public Static Int32 GetCharIndex (Char CDATA) { INT32 dwINDEX = -1, dwloop = 0; IF (m_acbase32map! = null) { For (dwloop = 0; dwloop { IF (m_acbase32map [dwloop] == CDATA) { Dwindex = dwloop; Break; } } } Return dwindex; } Public static string eNCode (byte [] abdata) { INT32 dwloop = 0, dwcharindex = 0, dwcharcount; CHAR [] acpart = null; Stringbuilder sboutput = NULL; IF (abdata == null || m_acbase32map == NULL || m_acbase32map.Length Return NULL; Try { DWcharcount = (int32) (Abdata.length / 5F * 8F) 1; SBOUTPUT = New StringBuilder (dwcharcount); Acpart = new char [8]; } Catch (exception) { } IF (acpart == null || sboutput == NULL) Return NULL; Dwcharcount = 0; For (dwloop = 0; dwloop { // Every 5 bytes is a unit, can communication to 8 chars // Data Format: // aaaaabbb bbcccccccd dddeeee effffggg ggghhhhhhhh Switch (abdata.length - dwloop) { Case 1: DWcharIndex = Abdata [dwloop] >> 3; // aaaaa Acpart [0] = m_acbase32map [dwcharindex]; DWcharIndex = (Abdata [dwloop] & 0x7) << 2; // bbb00 Acpart [1] = m_acbase32map [dwcharindex]; Dwcharcount = 2; Break; Case 2: DWcharIndex = Abdata [dwloop] >> 3; // aaaaa Acpart [0] = m_acbase32map [dwcharindex]; DWcharIndex = ((Abdata [dwloop] & 0x7) << 2) (Abdata [dwloop 1] >> 6); // bbbbb Acpart [1] = m_acbase32map [dwcharindex]; DWcharIndex = (Abdata [dwloop 1] & 0x3f) >> 1; // CCCCC Acpart [2] = m_acbase32map [dwcharindex]; DWcharIndex = Abdata [dwloop 1] & 0x1; // d0000 Acpart [3] = m_acbase32map [dwcharindex]; Dwcharcount = 4; Break; Case 3: DWcharIndex = Abdata [dwloop] >> 3; // aaaaa Acpart [0] = m_acbase32map [dwcharindex]; DWcharIndex = ((Abdata [dwloop] & 0x7) << 2) (Abdata [dwloop 1] >> 6); // bbbbb Acpart [1] = m_acbase32map [dwcharindex]; DWcharIndex = (Abdata [dwloop 1] & 0x3f) >> 1; // CCCCC Acpart [2] = m_acbase32map [dwcharindex]; DWcharindex = ((Abdata [dwloop 1] & 0x1) << 4) (Abdata [DWLOOP 2] >> 4); // DDDDD Acpart [3] = m_acbase32map [dwcharindex]; DWcharIndex = (Abdata [dwloop 2] & 0xF) << 1; // eeee0 Acpart [4] = m_acbase32map [dwcharindex]; Dwcharcount = 5; Break; Case 4: DWcharIndex = Abdata [dwloop] >> 3; // aaaaa Acpart [0] = m_acbase32map [dwcharindex]; DWcharIndex = ((Abdata [dwloop] & 0x7) << 2) (Abdata [dwloop 1] >> 6); // bbbbb Acpart [1] = m_acbase32map [dwcharindex]; DWcharIndex = (Abdata [dwloop 1] & 0x3f) >> 1; // CCCCC Acpart [2] = m_acbase32map [dwcharindex]; DWcharindex = ((Abdata [dwloop 1] & 0x1) << 4) (Abdata [DWLOOP 2] >> 4); // DDDDD Acpart [3] = m_acbase32map [dwcharindex]; DWcharindex = ((Abdata [dwloop 2] & 0xF) << 1) (Abdata [dwloop 3] >> 7); // eeeee Acpart [4] = m_acbase32map [dwcharindex]; DWcharIndex = (Abdata [DWLOOP 3] & 0x7f) >> 2; // fffff Acpart [5] = m_acbase32map [dwcharindex]; DWcharIndex = (Abdata [dwloop 3] & 0x3) << 3; // gg000 Acpart [6] = m_acbase32map [dwcharindex]; dwcharcount = 7; Break; DEFAULT: / /> = 5 DWcharIndex = Abdata [dwloop] >> 3; // aaaaa Acpart [0] = m_acbase32map [dwcharindex]; DWcharIndex = ((Abdata [dwloop] & 0x7) << 2) (Abdata [dwloop 1] >> 6); // bbbbb Acpart [1] = m_acbase32map [dwcharindex]; DWcharIndex = (Abdata [dwloop 1] & 0x3f) >> 1; // CCCCC Acpart [2] = m_acbase32map [dwcharindex]; DWcharindex = ((Abdata [dwloop 1] & 0x1) << 4) (Abdata [DWLOOP 2] >> 4); // DDDDD Acpart [3] = m_acbase32map [dwcharindex]; DWcharindex = ((Abdata [dwloop 2] & 0xF) << 1) (Abdata [dwloop 3] >> 7); // eeeee Acpart [4] = m_acbase32map [dwcharindex]; dwcharindex = (Abdata [dwloop 3] & 0x7f) >> 2; // ffffff Acpart [5] = m_acbase32map [dwcharindex]; DWcharIndex = ((Abdata [dwloop 3] & 0x3) << 3) (Abdata [dwloop 4] >> 5); // GGGGG Acpart [6] = m_acbase32map [dwcharindex]; DWcharIndex = Abdata [dwloop 4] & 0x1f; // HHHHH Acpart [7] = m_acbase32map [dwcharindex]; DWcharcount = 8; Break; } SBOUTPUT.APpend (acpart, 0, dwcharcount); } Return Sboutput.toString (); } Public static byte [] decode (String SDATA) { INT32 dwloop = 0, dwlength = 0; INT32 [] dwcharindex = null; BYTE [] Aboutput = NULL; CHAR [] acinput = null; IF (sdata == null || sdata == String.empty) Return NULL; acinput = sdata.tochararray (); IF (acinput == null) Return NULL; Try { DWLENGTH = (acinput.length / 8 * 5) 1; Aboutput = new byte [dwlength]; DWcharIndex = new int32 [8]; } Catch (Exception) { } IF (acinput == null) Return NULL; DWLENGTH = 0; For (dwloop = 0; dwloop { Switch (acinput.length - dwloop) { Case 1: DWcharindex [0] = getcharindex (acinput [dwloop]); Aboutput [dwlength] = (byte) (DWCHARINDEX [0] << 3); Break; Case 2: DWcharindex [0] = getcharindex (acinput [dwloop]); DWcharindex [1] = getcharindex (acinput [dwloop 1]); Aboutput [dwlength] = (byte) (dwcharindex [0] << 3 DWCHARINDEX [1] >> 2); Aboutput [dwlength 1] = (byte) ((DWCHARINDEX [1] & 0x3) << 6); Break; Case 3: DWcharindex [0] = getcharindex (acinput [dwloop]); DWcharIndex [1] = getcharindex (acinput [dwloop 1]); dwcharindex [2] = getcharindex (acinput [dwloop 2]); Aboutput [dwlength] = (byte) (dwcharindex [0] << 3 DWCHARINDEX [1] >> 2); Aboutput [DWLENGTH 1] = (byte) ((DWcharIndex [1] & 0x3) << 6 dwcharindex [2] << 1); Break; Case 4: DWcharindex [0] = getcharindex (acinput [dwloop]); DWcharindex [1] = getcharindex (acinput [dwloop 1]); DWcharindex [2] = getcharindex (acinput [dwloop 2]); DWcharIndex [3] = getcharindex (acinput [dwloop 3]); Aboutput [dwlength] = (byte) (dwcharindex [0] << 3 DWCHARINDEX [1] >> 2); Aboutput [DWLENGTH 1] = (Byte) ((DWCHARINDEX [1] & 0x3) << 6 dwcharindex [2] << 1 dwcharindex [3] >> 4); Aboutput [DWLENGTH 2] = (Byte) ((DWCHARINDEX [3] & 0xF) << 4); Break; Case 5: DWcharindex [0] = getcharindex (acinput [dwloop]); DWcharindex [1] = getcharindex (acinput [dwloop 1]); DWcharindex [2] = getcharindex (acinput [dwloop 2]); DWcharIndex [3] = getcharindex (acinput [dwloop 3]); DWcharindex [4] = getcharindex (acinput [dwloop 4]); Aboutput [dwlength] = (byte) (dwcharindex [0] << 3 DWCHARINDEX [1] >> 2); Aboutput [DWLENGTH 1] = (Byte) ((DWCHARINDEX [1] & 0x3) << 6 dwcharindex [2] << 1 dwcharindex [3] >> 4); Aboutput [DWLENGTH 2] = (Byte) (DWcharIndex [3] & 0xF) << 4 dwcharindex [4] >> 1); Aboutput [DWLENGTH 3] = (Byte) ((DWCHARINDEX [4] & 0x1) << 7); Break; Case 6: DWcharindex [0] = getcharindex (acinput [dwloop]); DWcharIndex [1] = getcharindex (acinput [dwloop 1]); dwcharindex [2] = getcharindex (acinput [dwloop 2]); DWcharIndex [3] = getcharindex (acinput [dwloop 3]); DWcharindex [4] = getcharindex (acinput [dwloop 4]); DWcharindex [5] = getcharindex (acinput [dwloop 5]); Aboutput [dwlength] = (byte) (dwcharindex [0] << 3 DWCHARINDEX [1] >> 2); Aboutput [DWLENGTH 1] = (Byte) ((DWCHARINDEX [1] & 0x3) << 6 dwcharindex [2] << 1 dwcharindex [3] >> 4); Aboutput [DWLENGTH 2] = (Byte) (DWcharIndex [3] & 0xF) << 4 dwcharindex [4] >> 1); Aboutput [DWLENGTH 3] = (Byte) ((DWcharIndex [4] & 0x1) << 7 dwcharindex [5] << 2); Break; Case 7: DWcharindex [0] = getcharindex (acinput [dwloop]); DWcharindex [1] = getcharindex (acinput [dwloop 1]); DWcharindex [2] = getcharindex (acinput [dwloop 2]); DWcharIndex [3] = getcharindex (acinput [dwloop 3]); DWcharindex [4] = getcharindex (acinput [dwloop 4]); DWcharindex [5] = getcharindex (acinput [dwloop 5]); DWcharindex [6] = getcharindex (acinput [dwloop 6]); Aboutput [dwlength] = (byte) (dwcharindex [0] << 3 DWCHARINDEX [1] >> 2); Aboutput [DWLENGTH 1] = (Byte) ((DWCHARINDEX [1] & 0x3) << 6 dwcharindex [2] << 1 dwcharindex [3] >> 4); Aboutput [DWLENGTH 2] = (Byte) (DWcharIndex [3] & 0xF) << 4 dwcharindex [4] >> 1); Aboutput [DWLENGTH 3] = (Byte) (DWCHARINDEX [4] & 0x1) << 7 dwcharindex [5] << 2 dwcharindex [6] >> 3); Aboutput [DWLENGTH 4] = (Byte) ((DWCHARINDEX [6] & 0x7) << 5); Break; DEFAULT: DWcharindex [0] = getcharindex (acinput [dwloop]); DWcharindex [1] = getcharindex (acinput [dwloop 1]); DWcharindex [2] = getcharindex (acinput [dwloop 2]); DWcharIndex [3] = getcharindex (acinput [dwloop 3]); DWcharindex [4] = getcharindex (acinput [dwloop 4]); DWcharindex [5] = getcharindex (acinput [dwloop 5]); DWcharindex [6] = getcharindex (acinput [dwloop 6]); DWcharindex [7] = getcharindex (acinput [dwloop 7]); Aboutput [dwlength] = (byte) (dwcharindex [0] << 3 DWCHARINDEX [1] >> 2); Aboutput [DWLENGTH 1] = (Byte) ((DWCHARINDEX [1] & 0x3) << 6 dwcharindex [2] << 1 dwcharindex [3] >> 4); Aboutput [DWLENGTH 2] = (Byte) (DWcharIndex [3] & 0xF) << 4 dwcharindex [4] >> 1); Aboutput [DWLENGTH 3] = (Byte) (DWCHARINDEX [4] & 0x1) << 7 dwcharindex [5] << 2 dwcharindex [6] >> 3); Aboutput [DWLENGTH 4] = (Byte) ((DWCHARINDEX [6] & 0x7) << 5 dwcharindex [8]); Break; } DWLENGTH = 5; } Return ABOUTPUT; } } } 3, post In fact, the author really thinks about Base24, what is the code of Base24, selling a Cat, saying in a few days. Of course, many of the sonsons have already known what is going on. ......