Java编写ASCII码转换

以下内容来自:https://jingyan.baidu.com/article/d8072ac45e1163ec94cefd78.html

一、ASCII编码查看器

直接上代码:

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import javax.swing.UIManager;
import javax.swing.border.EmptyBorder;
import javax.swing.border.EtchedBorder;

public class ASCIIViewer extends JFrame {

    private static final long serialVersionUID = -6067423561196663639L;
    private JPanel contentPane;
    private JTextField asciiTextField;
    private JTextField numberTextField;
    private JLabel label3;
    private JLabel label6;

    public static void main(String[] args) {
        try {
            UIManager.setLookAndFeel("com.sun.java.swing.plaf.nimbus.NimbusLookAndFeel");
        } catch (Throwable e) {
            e.printStackTrace();
        }
        
        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    ASCIIViewer frame = new ASCIIViewer();
                    frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public ASCIIViewer() {
        setTitle("ASCII\u7F16\u7801\u67E5\u770B\u5668");
        setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        setBounds(100, 100, 450, 150);
        contentPane = new JPanel();
        contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
        contentPane.setLayout(new BorderLayout(0, 0));
        setContentPane(contentPane);
        JPanel panel = new JPanel();
        contentPane.add(panel, BorderLayout.CENTER);
        panel.setLayout(new GridLayout(2, 1, 5, 5));
        JPanel asciiPanel = new JPanel();
        asciiPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        panel.add(asciiPanel);
        asciiPanel.setLayout(new GridLayout(1, 5, 5, 5));
        JLabel label1 = new JLabel("\u8F93\u5165\u5B57\u7B26\uFF1A");
        label1.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        asciiPanel.add(label1);
        asciiTextField = new JTextField();
        asciiTextField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        asciiPanel.add(asciiTextField);
        asciiTextField.setColumns(3);
        JLabel label2 = new JLabel("\u8F6C\u6362\u7ED3\u679C\uFF1A");
        label2.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        asciiPanel.add(label2);
        label3 = new JLabel("");
        label3.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        asciiPanel.add(label3);
        JButton toNumberButton = new JButton("\u8F6C\u6362");
        toNumberButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_toNumberButton_actionPerformed(e);
            }
        });
        toNumberButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        asciiPanel.add(toNumberButton);
        JPanel numberPanel = new JPanel();
        numberPanel.setBorder(new EtchedBorder(EtchedBorder.LOWERED, null, null));
        panel.add(numberPanel);
        numberPanel.setLayout(new GridLayout(1, 5, 5, 5));
        JLabel label4 = new JLabel("\u8F93\u5165\u6570\u5B57\uFF1A");
        label4.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        numberPanel.add(label4);
        numberTextField = new JTextField();
        numberTextField.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        numberPanel.add(numberTextField);
        numberTextField.setColumns(3);
        JLabel label5 = new JLabel("\u8F6C\u6362\u7ED3\u679C\uFF1A");
        label5.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        numberPanel.add(label5);
        label6 = new JLabel("");
        label6.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        numberPanel.add(label6);
        JButton toASCIIButton = new JButton("\u8F6C\u6362");
        toASCIIButton.addActionListener(new ActionListener() {
            public void actionPerformed(ActionEvent e) {
                do_toASCIIButton_actionPerformed(e);
            }
        });
        toASCIIButton.setFont(new Font("微软雅黑", Font.PLAIN, 16));
        numberPanel.add(toASCIIButton);
    }

    protected void do_toNumberButton_actionPerformed(ActionEvent e) {
        String ascii = asciiTextField.getText();
        int i = Character.codePointAt(ascii, 0);
        label3.setText("" + i);
    }

    protected void do_toASCIIButton_actionPerformed(ActionEvent e) {
        String number = numberTextField.getText();
        char[] a = Character.toChars(Integer.parseInt(number));
        label6.setText(new String(a));
    }
}
运行效果:


二、Java中文与ASCII码的转换

今天在研究Java中编码的时候,看到了Java中ascii码的强大。写了一个CoderUtils.java,以后会扩展它。

import java.io.File;  
import java.io.FileWriter;  
import java.io.IOException;  
import java.io.PrintWriter;  
  
/** 
 * @date 2009-3-11 
 * @author Xing,Xiudong 
 * @Email:xingxiuodng[at]gmail.com 
 * @index:http://blog.csdn.net/xxd851116 
 */  
public class CoderUtils {  
  
    public static char ascii2Char(int ASCII) {  
        return (char) ASCII;  
    }  
  
    public static int char2ASCII(char c) {  
        return (int) c;  
    }  
  
    public static String ascii2String(int[] ASCIIs) {  
        StringBuffer sb = new StringBuffer();  
        for (int i = 0; i < ASCIIs.length; i++) {  
            sb.append((char) ascii2Char(ASCIIs[i]));  
        }  
        return sb.toString();  
    }  
  
    public static String ascii2String(String ASCIIs) {  
        String[] ASCIIss = ASCIIs.split(",");  
        StringBuffer sb = new StringBuffer();  
        for (int i = 0; i < ASCIIss.length; i++) {  
            sb.append((char) ascii2Char(Integer.parseInt(ASCIIss[i])));  
        }  
        return sb.toString();  
    }  
  
    public static int[] string2ASCII(String s) {// 字符串转换为ASCII码  
        if (s == null || "".equals(s)) {  
            return null;  
        }  
  
        char[] chars = s.toCharArray();  
        int[] asciiArray = new int[chars.length];  
  
        for (int i = 0; i < chars.length; i++) {  
            asciiArray[i] = char2ASCII(chars[i]);  
        }  
        return asciiArray;  
    }  
  
    public static String getIntArrayString(int[] intArray) {  
        return getIntArrayString(intArray, ",");  
    }  
  
    public static String getIntArrayString(int[] intArray, String delimiter) {  
        StringBuffer sb = new StringBuffer();  
        for (int i = 0; i < intArray.length; i++) {  
            sb.append(intArray[i]).append(delimiter);  
        }  
        return sb.toString();  
    }  
  
    public static String getASCII(int begin, int end) {  
        StringBuffer sb = new StringBuffer();  
        for (int i = begin; i < end; i++) {  
            sb.append(i).append(":").append((char) i).append("/t");  
            // sb.append((char) i).append("/t");  
            if (i % 10 == 0) {  
                sb.append("/n");  
            }  
        }  
        return sb.toString();  
    }  
  
    public static String getCHASCII(int begin, int end) {  
        return getASCII(19968, 40869);  
    }  
  
    public static void showASCII(int begin, int end) {  
        for (int i = begin; i < end; i++) {  
            // System.out.print(i + ":" + (char) i + "/t");  
            System.out.print((char) i + "/t");  
            if (i % 10 == 0) {  
                System.out.println();  
            }  
        }  
    }  
  
    public static void showCHASCII() {  
        showASCII(19968, 40869);  
    }  
  
    public static void showIntArray(int[] intArray) {  
        showIntArray(intArray, ",");  
    }  
  
    public static void showIntArray(int[] intArray, String delimiter) {  
        for (int i = 0; i < intArray.length; i++) {  
            System.out.print(intArray[i] + delimiter);  
        }  
    }  
  
    public static void createFile(String filePathAndName, String fileContent)  
            throws IOException {  
  
        String filePath = filePathAndName;  
        filePath = filePath.toString();  
        File myFilePath = new File(filePath);  
        if (!myFilePath.exists()) {  
            myFilePath.createNewFile();  
        }  
        FileWriter resultFile = new FileWriter(myFilePath);  
        PrintWriter myFile = new PrintWriter(resultFile);  
        String strContent = fileContent;  
        myFile.println(strContent);  
        myFile.close();
        resultFile.close();
    }  
  
    public static void main(String[] args) throws IOException {  
  
        String s = "好好学习!天天向上!————笑的自然 2009年3月11日";  
        showIntArray(string2ASCII(s), " ");  
        System.out.println();  
        System.out.println(ascii2String(string2ASCII(s)));  
  
        createFile("C:/Users/huiqiang/Desktop/hui/console_ch.txt", getCHASCII(0, 50000));  
    }  
  
}  
运行结果:

Java中文与ASCII码的互相转换,能够下载任何字符。如下一瞥:

33:! 34:" 35:# 36:$ 37:% 38:& 39:' 40:( 
41:) 42:* 43:+ 44:, 45:- 46:. 47:/ 48:0 49:1 50:2 
51:3 52:4 53:5 54:6 55:7 56:8 57:9 58:: 59:; 60:< 
61:= 62:> 63:? 64:@ 65:A 66:B 67:C 68:D 69:E 70:F 
71:G 72:H 73:I 74:J 75:K 76:L 77:M 78:N 79:O 80:P 
81:Q 82:R 83:S 84:T 85:U 86:V 87:W 88:X 89:Y 90:Z 
91:[ 92:/ 93:] 94:^ 95:_ 96:` 97:a 98:b 99:c 100:d 
101:e 102:f 103:g 104:h 105:i 106:j 107:k 108:l 109:m 110:n 
111:o 112:p 113:q 114:r 115:s 116:t 117:u 118:v 119:w 120:x 
121:y 122:z 123:{ 124:| 125:} 126:~ 127: 
9471:⓿ 9472:─ 9473:━ 9474:│ 9475:┃ 9476:┄ 9477:┅ 9478:┆ 9479:┇ 9480:┈ 
9481:┉ 9482:┊ 9483:┋ 9484:┌ 9485:┍ 9486:┎ 9487:┏ 9488:┐ 9489:┑ 9490:┒ 
9491:┓ 9492:└ 9493:┕ 9494:┖ 9495:┗ 9496:┘ 9497:┙ 9498:┚ 9499:┛ 9500:├ 
9501:┝ 9502:┞ 9503:┟ 9504:┠ 9505:┡ 9506:┢ 9507:┣ 9508:┤ 9509:┥ 9510:┦ 
9511:┧ 9512:┨ 9513:┩ 9514:┪ 9515:┫ 9516:┬ 9517:┭ 9518:┮ 9519:┯ 9520:┰ 
9521:┱ 9522:┲ 9523:┳ 9524:┴ 9525:┵ 9526:┶ 9527:┷ 9528:┸ 9529:┹ 9530:┺ 
9531:┻ 9532:┼ 9533:┽ 9534:┾ 9535:┿ 9536:╀ 9537:╁ 9538:╂ 9539:╃ 9540:╄ 
9541:╅ 9542:╆ 9543:╇ 9544:╈ 9545:╉ 9546:╊ 9547:╋ 9548:╌ 9549:╍ 9550:╎ 
9551:╏ 9552:═ 9553:║ 9554:╒ 9555:╓ 9556:╔ 9557:╕ 9558:╖ 9559:╗ 9560:╘ 
9561:╙ 9562:╚ 9563:╛ 9564:╜ 9565:╝ 9566:╞ 9567:╟ 9568:╠ 9569:╡ 9570:╢ 
9571:╣ 9572:╤ 9573:╥ 9574:╦ 9575:╧ 9576:╨ 9577:╩ 9578:╪ 9579:╫ 9580:╬ 
9581:╭ 9582:╮ 9583:╯ 9584:╰ 9585:╱ 9586:╲ 9587:╳ 9588:╴ 9589:╵ 9590:╶ 
9591:╷ 9592:╸ 9593:╹ 9594:╺ 9595:╻ 9596:╼ 9597:╽ 9598:╾ 9599:╿ 9600:▀ 
9601:▁ 9602:▂ 9603:▃ 9604:▄ 9605:▅ 9606:▆ 9607:▇ 9608:█ 9609:▉ 9610:▊ 
9611:▋ 9612:▌ 9613:▍ 9614:▎ 9615:▏ 9616:▐ 9617:░ 9618:▒ 9619:▓ 9620:▔ 
12361:ぉ 12362:お 12363:か 12364:が 12365:き 12366:ぎ 12367:く 12368:ぐ 12369:け 12370:げ 
12371:こ 12372:ご 12373:さ 12374:ざ 12375:し 12376:じ 12377:す 12378:ず 12379:せ 12380:ぜ 
12381:そ 12382:ぞ 12383:た 12384:だ 12385:ち 12386:ぢ 12387:っ 12388:つ 12389:づ 12390:て 
12391:で 12392:と 12393:ど 12394:な 12395:に 12396:ぬ 12397:ね 12398:の 12399:は 12400:ば 
12401:ぱ 12402:ひ 12403:び 12404:ぴ 12405:ふ 12406:ぶ 12407:ぷ 12408:へ 12409:べ 12410:ぺ 
12411:ほ 12412:ぼ 12413:ぽ 12414:ま 12415:み 12416:む 12417:め 12418:も 12419:ゃ 12420:や 
12421:ゅ 12422:ゆ 12423:ょ 12424:よ 12425:ら 12426:り 12427:る 12428:れ 12429:ろ 12430:ゎ 
12431:わ 12432:ゐ 12433:ゑ 12434:を 12435:ん 12436:ゔ 12437:ゕ 12438:ゖ 12439:゗ 12440:゘ 
12441:゙ 12442:゚ 12443:゛ 12444:゜ 12445:ゝ 12446:ゞ 12447:ゟ 12448:゠ 12449:ァ 12450:ア 
12451:ィ 12452:イ 12453:ゥ 12454:ウ 12455:ェ 12456:エ 12457:ォ 12458:オ 12459:カ 12460:ガ 
12461:キ 12462:ギ 12463:ク 12464:グ 12465:ケ 12466:ゲ 12467:コ 12468:ゴ 12469:サ 12470:ザ 
12471:シ 12472:ジ 12473:ス 12474:ズ 12475:セ 12476:ゼ 12477:ソ 12478:ゾ 12479:タ 12480:ダ 
12481:チ 12482:ヂ 12483:ッ 12484:ツ 12485:ヅ 12486:テ 12487:デ 12488:ト 12489:ド 12490:ナ 
12491:ニ 12492:ヌ 12493:ネ 12494:ノ 12495:ハ 12496:バ 12497:パ 12498:ヒ 12499:ビ 12500:ピ 
12501:フ 12502:ブ 12503:プ 12504:ヘ 12505:ベ 12506:ペ 12507:ホ 12508:ボ 12509:ポ 12510:マ 
12511:ミ 12512:ム 12513:メ 12514:モ 12515:ャ 12516:ヤ 12517:ュ 12518:ユ 12519:ョ 12520:ヨ 
12521:ラ 12522:リ 12523:ル 12524:レ 12525:ロ 12526:ヮ 12527:ワ 12528:ヰ 12529:ヱ 12530:ヲ 
12531:ン 12532:ヴ 12533:ヵ 12534:ヶ 12535:ヷ 12536:ヸ 12537:ヹ 12538:ヺ 12539:・ 12540:ー 
12541:ヽ 12542:ヾ 12543:ヿ 12544:㄀ 12545:㄁ 12546:㄂ 12547:㄃ 12548:㄄ 12549:ㄅ 12550:ㄆ 
12551:ㄇ 12552:ㄈ 12553:ㄉ 12554:ㄊ 12555:ㄋ 12556:ㄌ 12557:ㄍ 12558:ㄎ 12559:ㄏ 12560:ㄐ 
12561:ㄑ 12562:ㄒ 12563:ㄓ 12564:ㄔ 12565:ㄕ 12566:ㄖ 12567:ㄗ 12568:ㄘ 12569:ㄙ 12570:ㄚ 
12571:ㄛ 12572:ㄜ 12573:ㄝ 12574:ㄞ 12575:ㄟ 12576:ㄠ 12577:ㄡ 12578:ㄢ 12579:ㄣ
1661:ٽ 1662:پ 1663:ٿ 1664:ڀ 1665:ځ 1666:ڂ 1667:ڃ 1668:ڄ 1669:څ 1670:چ 
1671:ڇ 1672:ڈ 1673:ډ 1674:ڊ 1675:ڋ 1676:ڌ 1677:ڍ 1678:ڎ 1679:ڏ 1680:ڐ 
1681:ڑ 1682:ڒ 1683:ړ 1684:ڔ 1685:ڕ 1686:ږ 1687:ڗ 1688:ژ 1689:ڙ 1690:ښ 
1691:ڛ 1692:ڜ 1693:ڝ 1694:ڞ 1695:ڟ 1696:ڠ 1697:ڡ 1698:ڢ 1699:ڣ 1700:ڤ 
1701:ڥ 1702:ڦ 1703:ڧ 1704:ڨ 1705:ک 1706:ڪ 1707:ګ 1708:ڬ 1709:ڭ 1710:ڮ 
1711:گ 1712:ڰ 1713:ڱ 1714:ڲ 1715:ڳ 1716:ڴ 1717:ڵ 1718:ڶ 1719:ڷ 1720:ڸ 
1721:ڹ 1722:ں 1723:ڻ 1724:ڼ 1725:ڽ 1726:ھ 1727:ڿ 1728:ۀ 1729:ہ 1730:ۂ 
1731:ۃ 1732:ۄ 1733:ۅ 1734:ۆ 1735:ۇ 1736:ۈ 1737:ۉ 1738:ۊ 1739:ۋ 1740:ی 
1741:ۍ 1742:ێ 1743:ۏ 1744:ې 1745:ۑ 1746:ے 1747:ۓ 1748:۔ 1749:ە 1750:ۖ 
9311:⑟ 9312:① 9313:② 9314:③ 9315:④ 9316:⑤ 9317:⑥ 9318:⑦ 9319:⑧ 9320:⑨ 
9321:⑩ 9322:⑪ 9323:⑫ 9324:⑬ 9325:⑭ 9326:⑮ 9327:⑯ 9328:⑰ 9329:⑱ 9330:⑲ 
9331:⑳ 9332:⑴ 9333:⑵ 9334:⑶ 9335:⑷ 9336:⑸ 9337:⑹ 9338:⑺ 9339:⑻ 9340:⑼ 
9341:⑽ 9342:⑾ 9343:⑿ 9344:⒀ 9345:⒁ 9346:⒂ 9347:⒃ 9348:⒄ 9349:⒅ 9350:⒆ 
9351:⒇ 9352:⒈ 9353:⒉ 9354:⒊ 9355:⒋ 9356:⒌ 9357:⒍ 9358:⒎ 9359:⒏ 9360:⒐ 
9361:⒑ 9362:⒒ 9363:⒓ 9364:⒔ 9365:⒕ 9366:⒖ 9367:⒗ 9368:⒘ 9369:⒙ 9370:⒚ 
9824:♠ 9825:♡ 9826:♢ 9827:♣ 9828:♤ 9829:♥ 9830:♦ 9786:☺ 9787:☻ 9788:☼ ◢ 9699:◣ 9700:◤ 9701:◥ 
12832:㈠ 12833:㈡ 12834:㈢ 12835:㈣ 12836:㈤ 12837:㈥ 12838:㈦ 12839:㈧ 12840:㈨
汉字部分:
19971:七 19972:丄 19973:丅 19974:丆 19975:万 19976:丈 19977:三 19978:上 19979:下 19980:丌 
19981:不 19982:与 19983:丏 19984:丐 19985:丑 19986:丒 19987:专 19988:且 19989:丕 19990:世 
19991:丗 19992:丘 19993:丙 19994:业 19995:丛 19996:东 19997:丝 19998:丞 19999:丟 20000:丠 
20001:両 20002:丢 20003:丣 20004:两 20005:严 20006:並 20007:丧 20008:丨 20009:丩 20010:个 
20011:丫 20012:丬 20013:中 20014:丮 20015:丯 20016:丰 20017:丱 20018:串 20019:丳 20020:临 
20021:丵 20022:丶 20023:丷 20024:丸 20025:丹 20026:为 20027:主 20028:丼 20029:丽 20030:举 
20031:丿 20032:乀 20033:乁 20034:乂 20035:乃 20036:乄 20037:久 20038:乆 20039:乇 20040:么 
20041:义 20042:乊 20043:之 20044:乌 20045:乍 20046:乎 20047:乏 20048:乐 20049:乑 20050:乒 
20051:乓 20052:乔 20053:乕 20054:乖 20055:乗 20056:乘 20057:乙 20058:乚 20059:乛 20060:乜 
20061:九 20062:乞 20063:也 20064:习 20065:乡 20066:乢 20067:乣 20068:乤 20069:乥 20070:书 
20071:乧 20072:乨 20073:乩 20074:乪 20075:乫 20076:乬 20077:乭 20078:乮 20079:乯 20080:买 
20081:乱 20082:乲 20083:乳 20084:乴 20085:乵 20086:乶 20087:乷 20088:乸 20089:乹 20090:乺 
20091:乻 20092:乼 20093:乽 20094:乾 20095:乿 20096:亀 20097:亁 20098:亂 20099:亃 20100:亄 
20101:亅 20102:了 20103:亇 20104:予 20105:争 20106:亊 20107:事 20108:二 20109:亍 20110:于 
20111:亏 20112:亐 20113:云 20114:互 20115:亓 20116:五 20117:井 20118:亖 20119:亗 20120:亘 
(运行代码查看生成的文件我只出来了汉字部分,而前面的特殊字符却没有,有时间再研究研究吧)


以上内容来自:http://blog.csdn.net/xxd851116/article/details/3981006

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值