Problems with encoding and decoding (encryption and decryption) commonly used in Android

Source: Internet
Author: User
Tags base64

1. URL Encoding

The purpose of encoding is to include special characters such as Chinese in the address.
Decoding is to restore the encoded content to the original content
The format is as follows%9c%3c%f3%98 rule:%hex_byte
is to convert the actual bytes to 16 binary for display
Encoded Urlencoder.encode (String str, string charset)
Decoding Urldecoder.encode (String str, string charset)

eg. %e6%88%91%e6%98%afvhly%ef%bc%8c%e4%bd%a0%e6%98%af%e8%b0%81%3f What does the string represent?

1 @Override2     protected voidonCreate (Bundle savedinstancestate) {3         Super. OnCreate (savedinstancestate);4 Setcontentview (r.layout.activity_main);5         6         /**Decoding Demo*/7         8         //the string before decoding9String str1= "%e6%88%91%e6%98%afvhly%ef%bc%8c%e4%bd%a0%e6%98%af%e8%b0%81%3f";Ten         //the string after decoding One         Try { AString str2= Urldecoder.decode (str1, "UTF-8"); -LOG.V ("TAG", "str2=" +str2);//str2= I'm vhly, who are you? -  the}Catch(unsupportedencodingexception e) { -             //TODO auto-generated Catch block - e.printstacktrace (); -         } +          -         /**Coding Demo*/ +String STR3 = "It ' s don!"; A         Try { atString STR4 = Urlencoder.encode (STR3, "UTF-8"); -LOG.V ("TAG", "str4=" +STR4);//str4=it%27s+don%21 -  -}Catch(unsupportedencodingexception e) { -             //TODO auto-generated Catch block - e.printstacktrace (); in         } -      to}
View Code

2.base64

Contains uppercase and lowercase letters, 10 numbers, and the encoding format of = +

1.base64 the ability to convert binary files to text files
2. Convert entire content to a string that can be highlighted
3. Contains encoding and decoding, mainly in the client and server map?
4. Code android.util.Base64.encodeToString (byte[], int)
5. Decoding Android.util.Base64.decode (String, int)


Common coding and decoding tools

Android comes with android.util.Base64
Apache Common Codec Class Library Base64

eg. 5l2g5piv6lcbpw== What is the content of this string?

1 ImportJava.net.URLDecoder;2 ImportJava.net.URLEncoder;3 4 Importandroid.util.Base64;5 @Override6     protected voidonCreate (Bundle savedinstancestate) {7         Super. OnCreate (savedinstancestate);8 Setcontentview (r.layout.activity_main);9         Ten         /**Decoding Demo*/ One          A         //the string before decoding -String str1= "5l2g5piv6lcbpw=="; -         //the string after decoding the         Try { -             byte[] bytes =Base64.decode (str1, base64.no_wrap); -String str2=NewString (Bytes, "UTF-8"); -LOG.V ("TAG", "str2=" +str2);//str2=, who are you? +  -  +}Catch(unsupportedencodingexception e) { A e.printstacktrace (); at         } -          -         /**Coding Demo*/ -String STR3 = "I Love android!"; -         Try { -String STR4 = base64.encodetostring (Str3.getbytes ("UTF-8"), base64.no_wrap); inLOG.V ("TAG", "str4=" +STR4);//STR4=SSBSB3ZLIEFUZHJVAWQH -  to}Catch(unsupportedencodingexception e) { +             //TODO auto-generated Catch block - e.printstacktrace (); the         } *      $}
View Code

Problems with encoding and decoding (encryption and decryption) commonly used in Android

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.