Java中文与ASCII码的转换

来源:互联网 发布:手机扫码软件 编辑:程序博客网 时间:2024/04/28 10:17

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

 

 

[java] view plaincopy
  1. package com.xingxd.study.test;  
  2.   
  3. import java.io.File;  
  4. import java.io.FileWriter;  
  5. import java.io.IOException;  
  6. import java.io.PrintWriter;  
  7.   
  8. /** 
  9.  * @date 2009-3-11 
  10.  * @author Xing,Xiudong 
  11.  * @Email:xingxiuodng[at]gmail.com 
  12.  * @index:http://blog.csdn.net/xxd851116 
  13.  */  
  14. public class CoderUtils {  
  15.   
  16.     public static char ascii2Char(int ASCII) {  
  17.         return (char) ASCII;  
  18.     }  
  19.   
  20.     public static int char2ASCII(char c) {  
  21.         return (int) c;  
  22.     }  
  23.   
  24.     public static String ascii2String(int[] ASCIIs) {  
  25.         StringBuffer sb = new StringBuffer();  
  26.         for (int i = 0; i < ASCIIs.length; i++) {  
  27.             sb.append((char) ascii2Char(ASCIIs[i]));  
  28.         }  
  29.         return sb.toString();  
  30.     }  
  31.   
  32.     public static String ascii2String(String ASCIIs) {  
  33.         String[] ASCIIss = ASCIIs.split(",");  
  34.         StringBuffer sb = new StringBuffer();  
  35.         for (int i = 0; i < ASCIIss.length; i++) {  
  36.             sb.append((char) ascii2Char(Integer.parseInt(ASCIIss[i])));  
  37.         }  
  38.         return sb.toString();  
  39.     }  
  40.   
  41.     public static int[] string2ASCII(String s) {// 字符串转换为ASCII码  
  42.         if (s == null || "".equals(s)) {  
  43.             return null;  
  44.         }  
  45.   
  46.         char[] chars = s.toCharArray();  
  47.         int[] asciiArray = new int[chars.length];  
  48.   
  49.         for (int i = 0; i < chars.length; i++) {  
  50.             asciiArray[i] = char2ASCII(chars[i]);  
  51.         }  
  52.         return asciiArray;  
  53.     }  
  54.   
  55.     public static String getIntArrayString(int[] intArray) {  
  56.         return getIntArrayString(intArray, ",");  
  57.     }  
  58.   
  59.     public static String getIntArrayString(int[] intArray, String delimiter) {  
  60.         StringBuffer sb = new StringBuffer();  
  61.         for (int i = 0; i < intArray.length; i++) {  
  62.             sb.append(intArray[i]).append(delimiter);  
  63.         }  
  64.         return sb.toString();  
  65.     }  
  66.   
  67.     public static String getASCII(int begin, int end) {  
  68.         StringBuffer sb = new StringBuffer();  
  69.         for (int i = begin; i < end; i++) {  
  70.             sb.append(i).append(":").append((char) i).append("/t");  
  71.             // sb.append((char) i).append("/t");  
  72.             if (i % 10 == 0) {  
  73.                 sb.append("/n");  
  74.             }  
  75.         }  
  76.         return sb.toString();  
  77.     }  
  78.   
  79.     public static String getCHASCII(int begin, int end) {  
  80.         return getASCII(1996840869);  
  81.     }  
  82.   
  83.     public static void showASCII(int begin, int end) {  
  84.         for (int i = begin; i < end; i++) {  
  85.             // System.out.print(i + ":" + (char) i + "/t");  
  86.             System.out.print((char) i + "/t");  
  87.             if (i % 10 == 0) {  
  88.                 System.out.println();  
  89.             }  
  90.         }  
  91.     }  
  92.   
  93.     public static void showCHASCII() {  
  94.         showASCII(1996840869);  
  95.     }  
  96.   
  97.     public static void showIntArray(int[] intArray) {  
  98.         showIntArray(intArray, ",");  
  99.     }  
  100.   
  101.     public static void showIntArray(int[] intArray, String delimiter) {  
  102.         for (int i = 0; i < intArray.length; i++) {  
  103.             System.out.print(intArray[i] + delimiter);  
  104.         }  
  105.     }  
  106.   
  107.     public static void createFile(String filePathAndName, String fileContent)  
  108.             throws IOException {  
  109.   
  110.         String filePath = filePathAndName;  
  111.         filePath = filePath.toString();  
  112.         File myFilePath = new File(filePath);  
  113.         if (!myFilePath.exists()) {  
  114.             myFilePath.createNewFile();  
  115.         }  
  116.         FileWriter resultFile = new FileWriter(myFilePath);  
  117.         PrintWriter myFile = new PrintWriter(resultFile);  
  118.         String strContent = fileContent;  
  119.         myFile.println(strContent);  
  120.         myFile.close();  
  121.         resultFile.close();  
  122.     }  
  123.   
  124.     public static void main(String[] args) throws IOException {  
  125.   
  126.         String s = "好好学习!天天向上!————笑的自然 2009年3月11日";  
  127.         showIntArray(string2ASCII(s), " ");  
  128.         System.out.println();  
  129.         System.out.println(ascii2String(string2ASCII(s)));  
  130.   
  131.         createFile("c://console_ch.txt", getCHASCII(050000));  
  132.     }  
  133.   
  134. }  

运行结果:

22909 22909 23398 20064 65281 22825 22825 21521 19978 65281 8212 8212 8212 8212 31505 30340 33258 28982 32 50 48 48 57 24180 51 26376 49 49 26085
好好学习!天天向上!————笑的自然 2009年3月11日

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: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 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

0 0
原创粉丝点击