Commons codec provides base64, Hex, metaphone, soundex, and Other encoding computations.
: Http://commons.apache.org/codec/
A. base64 encoding/Decoding
Package demo;
Import org. Apache. commons. codec. Binary. base64;
Public class base64test ...{
Public static void main (string [] ARGs )...{
Base64 base64 = new base64 ();
String STR = "Chinese ";
Byte [] enbytes = NULL;
String encodestr = NULL;
Byte [] debytes = NULL;
String decodestr = NULL;
Enbytes = base64.encode (Str. getbytes ());
Encodestr = new string (enbytes );
Debytes = base64.decode (enbytes );
Decodestr = new string (debytes );
System. Out. println ("Before encoding:" + Str );
System. Out. println ("Encoded:" + encodestr );
System. Out. println ("decoded:" + decodestr );
}
}
B. Hex Encoding/Decoding
Package demo;
Import org. Apache. commons. codec. decoderexception;
Import org. Apache. commons. codec. Binary. HEX;
Public class hextest ...{
Public static void main (string [] ARGs) throws decoderexception ...{
Hex hex = new hex ();
String STR = "Chinese ";
Char [] enbytes = NULL;
String encodestr = NULL;
Byte [] debytes = NULL;
String decodestr = NULL;
Enbytes = Hex. encodehex (Str. getbytes ());
Encodestr = new string (enbytes );
Debytes = Hex. decodehex (enbytes );
Decodestr = new string (debytes );
System. Out. println ("Before encoding:" + Str );
System. Out. println ("Encoded:" + encodestr );
System. Out. println ("decoded:" + decodestr );
}
}
C. metaphone and soundex Encoding
Package demo;
Import org. Apache. commons. codec. Language. metaphone;
Import org. Apache. commons. codec. Language. refinedsoundex;
Import org. Apache. commons. codec. Language. soundex;
Public class extends agetest ...{
Public static void main (string [] ARGs )...{
Metaphone = new metaphone ();
Refinedsoundex = new refinedsoundex ();
Soundex = new soundex ();
For (INT I = 0; I <2; I ++ )...{
String STR = (I = 0 )? "Resume": "resin ";
String mstring = NULL;
String rstring = NULL;
String sstring = NULL;
Try ...{
Mstring = metaphone. encode (STR );
Rstring = refinedsoundex. encode (STR );
Sstring = soundex. encode (STR );
} Catch (exception ex )...{
;
}
System. Out. println ("Original:" + Str );
System. Out. println ("metaphone:" + mstring );
System. Out. println ("refinedsoundex:" + rstring );
System. Out. println ("soundex:" + sstring + "/N ");
}
}
}
Metaphone creates the same key for words with similar pronunciation, which is more accurate than soundex, but metaphone does not have a fixed length. soundex adds three numbers to the first English text. this is usually used in software development like audio comparison and MP3.