首页 > 代码库 > 数据的编码和解码--java例子

数据的编码和解码--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例子