Jakarta-common-codec usage notes

Source: Internet
Author: User

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.

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.