--java example of encoding and decoding data

Source: Internet
Author: User
Tags gettext

Yesterday borrowed a "Web Design Experiment Tutorial (Java language)", and then read the first chapter, a swing example, so for everyone to share!

As for the coding and decoding of the data, I think it has been explained very clearly in the example, and two methods have been done.

Turn it into a 16-binary.

//UTF8 decodingBtnutf8decode =NewJButton ("utf8\u89e3\u7801\u7801\u7ed3\u679c"); Btnutf8decode.addmouselistener (NewMouseadapter () {@Override Public voidmouseclicked (MouseEvent e) {String hexstr=Tfbytes.gettext (); String[] STRs= Hexstr.split (""); byte[] bytes =New byte[Strs.length];  for(inti=0;i<strs.length; i++) {Bytes[i]= (byte) integer.valueof (strs[i],16). Intvalue (); } string String= ""; Try{string=NewString (Bytes, "UTF-8"); } Catch(unsupportedencodingexception E2) {//Todo:handle ExceptionE2.printstacktrace (); }finally{Tfcharsutf8.settext (string);        }            }        }); 

//GBK decodingBtngbkdecode =NewJButton ("gbk\u89e3\u7801\u7ed3\u679c"); Btngbkdecode.addmouselistener (NewMouseadapter () {@Override Public voidmouseclicked (MouseEvent e) {String hexstr=Tfbytes.gettext (). toString (). Trim (); String[] STRs= Hexstr.split (""); byte[] Array =New byte[Strs.length];  for(inti=0; i<strs.length; i++) {Array[i]= (byte) integer.valueof (strs[i],16). Intvalue (); } Charset CS= Charset.forname ("GBK"); Charbuffer Buffer=Cs.decode (Java.nio.ByteBuffer.wrap (array));            Tfcharsgbk.settext (Buffer.tostring ()); }        });

//UTF8 EncodingBtnutf8encode =NewJButton ("utf8\u7f16\u7801\u7ed3\u679c"); Btnutf8encode.addmouselistener (NewMouseadapter () {@Override Public voidmouseclicked (MouseEvent e) {String str=Tfchars.gettext (); String hexstr= ""; Try {                    byte[] bytes = Str.getbytes ("UTF-8");  for(inti=0; i<bytes.length; i++) {hexstr+ = Integer.tohexstring (Bytes[i] & 0xFF). toUpperCase () + ""; }                } Catch(unsupportedencodingexception E2) {//Todo:handle ExceptionE2.printstacktrace (); }finally{tfbytesutf8.settext (HEXSTR); }            }        });
//Code GBKJButton Btngbkencode =NewJButton ("gbk\u7f16\u7801\u7ed3\u679c"); Btngbkencode.addmouselistener (NewMouseadapter () {@Override Public voidmouseclicked (MouseEvent e) {String str=Tfchars.gettext (). toString (). Trim (); Charset CS= Charset.forname ("GBK"); Java.nio.ByteBuffer Buffer=Cs.encode (str); String hexstr= "";  while(buffer.remaining () > 0) {hexstr+ = Integer.tohexstring (Buffer.get () & 0xFF). toUpperCase () + "";            } tfbytesgbk.settext (HEXSTR); }        }); 

--java example of encoding and decoding data

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.