日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > java >内容正文

java

Java编写ASCII码转换

發布時間:2023/12/20 java 24 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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

總結

以上是生活随笔為你收集整理的Java编写ASCII码转换的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。