数据的编码和解码--java例子

Posted 林嘉瑜

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了数据的编码和解码--java例子相关的知识,希望对你有一定的参考价值。

  昨天借了一本《网络程序设计实验教程(java语言)》,然后看了第一章,一个Swing例子,于是为大家分享一下!

  关于数据的编码与解码,我觉得就例子而言已经交待得非常清楚了,两种方法做的。

  将其转为16进制来弄的

//utf8解码
        btnUTF8Decode = new JButton("UTF8\\u89E3\\u7801\\u7801\\u7ED3\\u679C");
        btnUTF8Decode.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String hexStr = tfBytes.getText();
                String[] strs = hexStr.split(" ");
                byte[] bytes = new byte[strs.length];
                for (int i=0;i<strs.length; i++){
                    bytes[i] = (byte) Integer.valueOf(strs[i],16).intValue();
                }
                String string = "";
                try {
                    string = new String(bytes,"UTF-8");
                } catch (UnsupportedEncodingException e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }finally {
                    tfCharsUTF8.setText(string);
                }
            }
        });
        

 

//gbk解码
        btnGBKDecode = new JButton("GBK\\u89E3\\u7801\\u7ED3\\u679C");
        btnGBKDecode.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String hexStr = tfBytes.getText().toString().trim();
                String[] strs = hexStr.split(" ");
                byte[] array = new byte[strs.length];
                for (int i=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编码
        btnUTF8Encode = new JButton("UTF8\\u7F16\\u7801\\u7ED3\\u679C");
        btnUTF8Encode.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                String str = tfChars.getText();
                String hexStr = "";
                try {
                    byte[] bytes = str.getBytes("UTF-8");
                    for (int i=0; i<bytes.length; i++){
                        hexStr += Integer.toHexString(bytes[i] & 0xFF).toUpperCase()+" ";
                    }
                } catch (UnsupportedEncodingException e2) {
                    // TODO: handle exception
                    e2.printStackTrace();
                }finally {
                    tfBytesUTF8.setText(hexStr);
                }
            }
        });
//编码GBK
        JButton btnGBKEncode = new JButton("GBK\\u7F16\\u7801\\u7ED3\\u679C");
        btnGBKEncode.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(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例子的主要内容,如果未能解决你的问题,请参考以下文章

使用OpenSSL进行Base64编码和解码

java web开发,页面处理Base64编码

Java 之 IO操作—— 文本数据的编码和解码

如何以正确的方式编码和解码字符[重复]

PHP解码url编码问题

java中编码和解码的问题